load subview over self.view - iphone

I am trying to add a UIView over the current view, but having no luck.. I am sure my code is correct but not sure what else to check, what else should I be thikning about when inserting subviews
this is what I have added into viewDidLoad
//..
cellContainer = [[UIView alloc] init];
cellContainer.frame = CGRectMake(0.0, 480.0, 320.0, 300.0);
[self.view addSubview:cellContainer];
self.view.backgroundColor = [UIColor greenColor];
//..
cellContainer is declared in the header as a UIView... any help would be appreciated.

cellContainer is likely getting added to the view hierarchy of of the UIViewController, but you've set the frame such that it is offscreen. Try this:
cellContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[self.view addSubview:cellContainer];
self.view.backgroundColor = [UIColor greenColor];
This will display cellContainer at the top left of the view, extending 320px in the right-x direction, and 300px in the down-y direction.

Related

change sub UIView background color

I'm trying to add subview in my ViewController using this method
- (void)viewDidLoad
{
[super viewDidLoad];
addCourse = [[UIView alloc]init];
addCourse.backgroundColor = [UIColor redColor];
[addCourse.layer setCornerRadius:50.0f];
[addCourse.layer setMasksToBounds:YES];
}
Where addCourse is my subview but the background color is still white. and I'm using storyboard and added the subview with it and linked the View with viewcontroller and I did write the IBOutlet and the property and synthesize but still I couldn't find a way to change the background color.
How I can change it ?
If you already linked with the ViewController then you just need to remove the below line from your code
addCourse = [[UIView alloc]init];
Because, it create the new instance of your subView.
You haven't added addCourse to your view controller as as I can see. replace your viewDidLoad method with:
- (void)viewDidLoad
{
[super viewDidLoad];
addCourse = [[UIView alloc]init];
addCourse.backgroundColor = [UIColor redColor];
[addCourse.layer setCornerRadius:50.0f];
[addCourse.layer setMasksToBounds:YES];
//ADDED THIS LINE
self.view = addCourse;
}
See I have updated your code (Just you need to Set View's Frame) and I got the Result
Please! Update your code this way
- (void)viewDidLoad
{
[super viewDidLoad];
//custom view
UIView *addCourse = [[UIView alloc]init];
CGRect myViewframe = CGRectMake(10.0f, 90.0f, 100.0f, 100.0f);
addCourse.frame=myViewframe;
addCourse.backgroundColor = [UIColor redColor];
[addCourse.layer setCornerRadius:50.0f];
[addCourse.layer setMasksToBounds:YES];
[self.view addSubview:addCourse];
}
and Here is the result screen
addCourse = [[UIView alloc]initWithFrame:CGRectMake(x, y, w, h)];
addCourse.backgroundColor = [UIColor redColor];
[addCourse.layer setCornerRadius:50.0f];
[addCourse.layer setMasksToBounds:YES];
[self.view addSubview:addCourse];

Adding UIScrollView to view and sending it to the back

I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:
Here is my code right at the end of -viewWillAppear method (code from my View Controller):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:
detailsLabel.text = #"Some long text taken from databse";
[detailsLabel sizeToFit];
detailsLabel.numberOfLines = 0;
detailsLabel.lineBreakMode = UILineBreakModeWordWrap;
What I am doing wrong? How do I put UIScrollView below all other elements of my main view?
Try this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view insertSubview:scroll atIndex:0];
...
[self.view insertSubview:detailsLabel atIndex:1];
Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .
I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];
Option One:
Declare detailsLabel in .h:
IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD
give it property and Synthesise in .m.
here in viewDidLoad declare:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
[scroll addSubview:detailsLabel];
// IT SHOULD WORK.
Option two:
If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

Scroll view with an image is not scrolling

i added an image to the scroll view. but its not scrolling...
pls help out what is the problem..
Thanks.
boxImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 372)];
boxImage.image = [UIImage imageNamed:#"chapter1box.png"];
textScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 175, 320, 755)];
scrollTextImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 775)];
scrollTextImg.image = [UIImage imageNamed:#"chapter1narrationtext.png"];
textScroll.backgroundColor = [UIColor clearColor];
textScroll.scrollEnabled = YES;
textScroll.pagingEnabled = YES;
textScroll.directionalLockEnabled = YES;
textScroll.autoresizesSubviews = YES;
textScroll.contentSize = CGSizeMake(320, 775);
[self.view addSubview:boxImage];
[boxImage addSubview:textScroll];
[textScroll addSubview:scrollTextImg];
UIImageView has userInteractionEnabled property set to NO by default. Adding
boxImage.userInteractionEnabled = YES;
should help.
The scroll view size has to be smaller than the size of it's content for scrolling to work.
in the code i add the scrollview to an image view
i added it to the self.view its working now.
cant we add a scroll view to an imageview..?

Set up view in code that changes with orientation

I have set up an application and used some of the basic code from "The iPhone Developer's Cookbook" to create a simple view. The application works correctly in portrait mode, but when it is rotated, the dimensions of the boxes stay the same. Here is my code:
- (void)loadView {
//Create the full application frame (minus the status bar) - 768, 1004
CGRect fullRect = [[UIScreen mainScreen] applicationFrame];
//Create the frame of the view ie fullRect-(tabBar(49)+navBar(44)) - 768, 911
CGRect viewRect = CGRectMake(0, 64, fullRect.size.width, fullRect.size.height-93.0);
//create the content view to the size
contentView = [[UIView alloc] initWithFrame:viewRect];
contentView.backgroundColor = [UIColor whiteColor];
// Provide support for autorotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
[contentView release];
// reset the origin point for subviews. The new origin is 0,0
viewRect.origin = CGPointMake(0.0f, 0.0f);
// Add the subviews, each stepped by 32 pixels on each side
UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 32.0f, 32.0f)];
subview.backgroundColor = [UIColor lightGrayColor];
[contentView addSubview:subview];
[subview release];
subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 64.0f, 64.0f)];
subview.backgroundColor = [UIColor darkGrayColor];
[contentView addSubview:subview];
[subview release];
subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 96.0f, 96.0f)];
subview.backgroundColor = [UIColor blackColor];
[contentView addSubview:subview];
[subview release];
}
Is there any way to have this reload with the new dimensions when it is rotated or a better way of accommodating the orientation change?
Thanks
JP
I may be misunderstanding your problem. By "boxes", do you mean the subviews?
If so, the subviews will retain their apparent dimensions when rotated because they are squares.
In any case, viewDidLoad is only called when a view is first initialized, usually from a nib. If you need to make changes to a view when it rotates, you need to make the changes in willRotateToInterfaceOrientation:duration: and/or didRotateFromInterfaceOrientation:.
To change the dimensions of any view, simply reset its frame.
object.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;