UIViewController not filling screen - iphone

I have a UINavigationController that pushes on another UIViewController. In this UIViewController I am going to show a UITableView when in portrait mode and another view in landscape mode.
Therefore in my viewDidLoad I am creating UIView and then adding 2 ViewControllers to this. My problem is that when it loads up I get the following white margin at the top.
I think this is because of (in my Step 3 below ) the
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];
is not returning the full screen, minus the navigation bar. Is this right? If so how can I make it return the full size so there is no white margin?
- (void)viewDidLoad
{
[super viewDidLoad];
// Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];
// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc]
initWithNibName:#"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];
// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];
// Step 4 - Add the vertical view to the containerView
// and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease];
[[self view] addSubview:containerView];
[containerView addSubview:[tableDataViewController view]];
// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];
}
Many thanks
Mike

I think you have an issue with your frame offsets. With the navigation bar enabled the rect you get in appFrame has a y offset of 44.f (the navigation bar's height) - check with NSLog and see if that's true.
Because you are setting the frame of a view that will be placed at the origin it should have x and y origins set to zero. You can do this in a safer manner by checking
CGFloat navHeight = navController.navigationBarHidden ? 0 :
navController.navigationBar.frame.size.height;
In fact I think using the bounds property of [UIScreen mainScreen] may be a better overall solution. It will come with the origin and size set correctly and you shouldn't need to check the presence of the navigation bar.
Check what's going on:
CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(#"%#", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(#"%#", NSStringFromCGRect(screenFrame));

There are a couple things going on. Above all, frame describes a view's location in its superview's coordinate system.
containerView seems to be added as a subview of a custom view, and its coordinate system origins below the navigation bar, not the application frame.
You want to fill the custom view, so the frame of containerView should be something like
CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);

Related

What piece am I missing to set an iOS background?

I have:
[A series of if statements sets backgroundImage to be something like [UIImageNamed #"Background-Default-Landscape"]; am I right to assume that the device will look for the file named Background-Default-Landscape#2x.png if it is a Retina display?]
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
Right now my app is launching and once it loads displays a white screen, not a background specified (none of the backgrounds are solid white).
What am I missing to draw a background before I start putting things on the background, further below in the code?
Thanks,
--EDIT--
As far as code, I have:
- (void) renderScreen
{
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
UIImage *backgroundImage = nil;
if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
{
backgroundImage = [UIImage imageNamed:#"Background-Default-Portrait"];
}
// Other such statements cut.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
Then below that are statements to draw on the background.
This method is called when the initial view loads and when it is rotated:
- (void)viewDidLoad
{
[super viewDidLoad];
[self renderScreen];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
}
The behavior is that the correct initial screen loads, then it fades to white and nothing interesting seems to happen after that.
--EDIT--
At the top, I have:
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
When I NSLog the height and width, for a retina device in landscape mode, I get a height of 1024 and a width of 768. The image displayed is a rotation of the portrait image; if I turn the device on the side the image neatly fills the whole screen but as-is the background displays a horizontally squeezed image.
Should I do anything different to be correctly obtaining height and width? I would expect for a device in landscape orientation the height would be 768 and the width would be 1024.
Every time renderScreen fires it will add two more subviews to the current controller's main view:
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
And this fires every time you rotate the device.
At least have the containerView as a property and replace it.
#property (nonatomic, strong) UIView *containerView;
and inside - (void) renderScreen
[self.containerView removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
[self.view addSubview:self.containerView];
You could also add:
[self.view setNeedsDisplay];
So that the view is redrawn.

The frame size of the view is not changing on rotation when the view is created with loadView

I have UIViewController without a xib, and I'm using loadView to build my UI that creates and adds two scroll views. The thing is, the main view frame size is not changing when rotation happens. I mean, I'm setting initial frame size for the main view in loadView (portrait mode: frame size width = 320, height = 480). After rotation to landscape orientation, the view main frame size isn't changing and is the same as in portrait mode. Here's my loadView:
-(void)loadView{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)] autorelease];
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.scrollView1 = [[[UIScrollView alloc] init] autorelease];
self.scrollView1.frame = CGRectMake...
[self.view addSubview:self.scrollView1];
self.scrollView2 = [[[UIScrollView alloc] init] autorelease];
self.scrollView2.frame = CGRectMake...
[self.view addSubview:self.scrollView2];
I have also set autoresizingMask for the view to expand or shrink to fit the screen. But I'm getting the same width and height values when debugging that in console. I need to get a new size because I need to reposition my two scrollViews when rotation happens, for example to shrink the height and expand the width on the scrollViews on rotation to landscape mode. I could do manually in shouldRotate, just curious about how it should be done in proper way.
I have noticed that self.view.frame size does not change but self.view.bounds does on rotation, and bounds represent correct values with respect to current interface orientation.

How to layout a view whose frame depends of the orientation

Consider a view that has to aligned to the bottom of the screen and whose width must fill the screen.
Additionally, the height of the view depends of the orientation. In portrait orientation the height of the view must be 200px and in landscape the height must be 100px.
What is the best way to do this without assuming anything about the parent view size (ie: don't know if it has status bar or not, maybe there's a tab bar, etc.)?
This is my current code, that doesn't work:
- (void)viewDidLoad
{
[super viewDidLoad];
myView = [[UIView alloc] init];
CGSize myViewSize = CGSizeMake(self.view.frame.size.width, 200);
// Align bottom
float myViewY = self.view.frame.origin.y + self.view.frame.size.height - myViewSize.height;
myView.frame = CGRectMake(0, myViewY, myViewSize.width, myViewSize.height);
myView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
BOOL landscape = UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, myView.frame.size.width, landscape ? 100 : 200);
}
Try using UIWindow object as given below:
[[UIApplication sharedApplication] keyWindow].screen.applicationFrame.size.height
Apple documentation for applicationFrame reads as below:
This property contains the screen bounds minus the area occupied by the status bar, if it is visible. Using this property is the recommended way to retrieve your application’s initial window size. The rectangle is specified in points

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

Setting the size of a UIView

I have a UIViewController subclass to control a UIView that I want to add to a UIScrollView. I only want the view to be 100px high, but when I add it to the scroll view it gets made 460px high, ignoring the frame size I set:
MyViewController *vc = [[MyViewController alloc] init];
vc.view.frame = CGRectMake(0, 0, 320, 100);
myScrollView.autoresizesSubviews = NO
[myScrollView addSubview:vc.view];
[vc release];
I have set the scroll view to not autoresize subviews but it seems this is still happening! What can I do?
I have also tried setting the frame size inside loadView: in the UIViewController (which is where I will add all my controls and will need access to the size of the view) but that doesnt work either!
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
self.view.frame = CGRectMake(0, 0, 320, 100); // still doesnt work
}
Any help would be greatly appreciated!
You are using loadView incorrectly, im even suprised you see a view (you shouldnt since in load view you arent assigns the vc view to anything), in loadView you must assign your view to a new UIView i nstance, anyway, you should be doing the same but in viewDidLoad instead of load view, that might work for you
Here is a snippet of how I do it. Note that the origin is with respect to the view you are adding to (in my case 'self').
appRect.origin=CGPointMake(0.0, 0.0);// origin
appRect.size = CGSizeMake(320.0f, 100.0f); //size
CGRect frame = CGRectInset(appRect, 0.0f, 0.0f);
gv=[[GraphicsView alloc] initWithFrame:appRect object:[model me]];
[gv setFrame:frame];
[self.view addSubview:gv];
[gv release];