Top of View Cut Off (under status bar, interface builder)
On this, my first iPhone project after 30+ years of C++, ever since I went through the tutorial "Your First iPhone Application", the top of my view has been cut off, hidden under the status bar. Meanwhile, the bottom of the screen has a blank area not covered by my test background color. I followed all the tutorial instructions; used Interface Builder. In Interface Builder, the view with test-colored background, appears correctly BELOW the status bar, and fills the whole screen. When run in the simulator or on a device, however, the view is Y pixels higher, hiding partially under the status bar and not filling out to the bottom of the screen.
I didn't do any special X,Y positioning of the view. I would have thought that Interface Builder would have positioned things correctly relative to the status bar, and I would have thought that the tutorial provided code snippets that did things appropriately as well.
It seems to me that I need to compensate by moving the view down. However, I shouldn't need to do this, having followed the tutorial. So I'm wondering what I'm missing.
Just in case, here's my appdelegate code that creates the view controller. Also, there's no special code in the view controller itself, that would be germane to this question.
...
// Create main view controller
mainViewController = [[MainViewController alloc]
initWithNibName:@"MainViewController" //forum note, can't get this to indent right on forum
bundle:[NSBundle mainBundle]]; //forum note, can't get this to indent right on forum
[self setMainViewController:mainViewController];
// Setup the main view
[window addSubview:[mainViewController view]];
...
[window makeKeyAndVisible];
Please advise as to what I'm missing. I searched the forum but couldn't find any comments on this. (Hey, it's happening to everyone and nobody's mentioning it! I don't think so. I must have done something wrong...)
Thanks,
Dr.Lightning
Last edited by Dr.Lightning; 02-14-2010 at 07:42 PM.
I did not explicitly change ANYTHING regarding the view location, orientation, or status bar. This view location problem is how things came "out of the box" from interface builder and following the tutorial.
I'd like to know why this happened and correct it, rather than overriding the situation by explicitly moving the window.
2) By the time you finish the "Inspecting the Nib File" page (5th page), when you run the app in the simulator you'll see a purple background.
3) However, there's a white bar below the purple background. Go back to interface builder and add label text at the very top left and bottom right corners of the MyViewController.xib view. Now when you run the simulator, you find that your top left text is under the status bar, and the bottom right text is above the white bar.
4) Further go back and use the interface builder on the MainWindow.xib that's still white. Add top left and bottom right text to it as well. (I used 48 point so it's tall enough to both position at the very top of the view, as well as later hang down partially below when it's hidden under the status bar.) Run the simulator. You'll see that the white bar at the bottom of the screen is the MainWindow showing at the bottom, and the bottom right text shows as well.
5) Therefore, the purple MyViewController view is positioning under the status bar, and is only high enough assuming it's NOT positioned under the status bar. Therefore, it ends up short, not reaching the bottom of the screen. As a result, you can still see the MainWindow view under it.
I don't think I'm doing anything wrong. Perhaps folks don't notice this when they later have a white background and don't position hello world stuff right up against the edges of the screen.
Code:
//
// HellowWorldViewAppDelegate.h
// HellowWorldView
//
// Created by Helmut Forren on 2/15/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@class MyViewController;
@interface HellowWorldViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *myViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) MyViewController *myViewController;
@end
Code:
//
// HellowWorldViewAppDelegate.m
// HellowWorldView
//
// Created by Helmut Forren on 2/15/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "MyViewController.h";
#import "HellowWorldViewAppDelegate.h"
@implementation HellowWorldViewAppDelegate
@synthesize window;
@synthesize myViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[myViewController release];
[window release];
[super dealloc];
}
@end
Code:
//
// Prefix header for all source files of the 'HellowWorldView' target in the 'HellowWorldView' project
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
Code:
//
// main.m
// HellowWorldView
//
// Created by Helmut Forren on 2/15/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Code:
//
// MyViewController.h
// HellowWorldView
//
// Created by Helmut Forren on 2/15/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
}
@end
Code:
//
// MyViewController.m
// HellowWorldView
//
// Created by Helmut Forren on 2/15/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MyViewController.h"
@implementation MyViewController
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
I didn't include the .xib XML because it makes the post too long for the forum software to accept.
I've just looked at both, and honestly, i haven't had very good luck with apple tutorials in the past. I would check out others, like icodeblog, or youtube.
give those a try and I think you will have less problems.
after aViewController is assigned (allocated) causes the MyViewController purple background to continue to the bottom of the screen, covering up the white of MainWindow. However, it does NOT reposition (move) the text labels down out from under the scroll bar. Therefore, while I could manually position the top left text in interface builder to not be under the scroll bar, I can NOT manually position the bottom right next any further lower, and therefore can't use the bottom region of the screen at all.
- (void)viewWillAppear:(BOOL)animated {
// to fix the controller showing under the status bar
self.view.frame = [[UIScreen mainScreen] applicationFrame];
}
- (void)viewWillAppear:(BOOL)animated {
// to fix the controller showing under the status bar
self.view.frame = [[UIScreen mainScreen] applicationFrame];
}
==CONFIRMED== The above suggestion worked. In detail, in the context of the most simple Hello World example, which is in the context of the code I posted higher up, here's what I did. I found in MyViewController.m the function viewDidLoad, that was put there automagically by XCode when the class was created, but was commented out. I removed the comment-out to restore the code, then added the appropriate code inside:
Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.frame = [[UIScreen mainScreen] applicationFrame];
[super viewDidLoad];
}
And that fixed the problem.
==WHAT THIS MEANS== in essence is that the tutorial falls short of being purely "correct" and should have included this or a similar fix. Perhaps ==MORE TO THE POINT==, XCode should have put that code in there, running not commented out.
I love you, Doctor. This has bugged me for the whole year I've been working on this device. I've hacked and hacked and gotten it looking right each project but I always felt dirty. I can't believe that at the start of your program you have to tell the main view to run at its OWN resolution.