iPhone: Weird space at the top of UINavigationController
I’m having a strange problem with adding a UINavigationController to my iPhone application. I add the controller as follows:
myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil]; myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController]; UIView *finalView = myeNavigationViewController.view; [self.view addSubview:finalView];
All seems to work as planned except I get a weird white space at the top of my view between the status bar and the UINavigationController title bar.
alt text http://img.ioscake.com/iphone/problem.png
I’ve searched online but don’t really know what to search for. Has anyone else had this problem? Can you point me in the direction of some help?
Thanks in advance.
- How to change the font and text color of the title of UINavigationBar
- CALayer - Shadow causes a performance hit?
- iOS: UISplitViewController cannot be pushed to UINavigationController
- How to tell when back button is pressed in a UINavigationControllerStack
- NavigationBar becomes black on pop
- UINavigationController alternatives
6 Solutions Collect From Internet About “iPhone: Weird space at the top of UINavigationController”
Check out the answers in this question:
Not sure why UIView is being nudged up by around 10px
What does the line
UIView *finalView = myeNavigationViewController.view;
add to the code? It’s redundant as you can add the view directly without assigning it to a UIView first – plus it’s incorrect as it references the myNavigationController and not navigationController..
I tend to do this
myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil]; myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController]; [navigationController.view setFrame: [self.view bounds]]; navigationController.delegate = self; [self.view addSubview:[navigationController view]];
Setting the frame to the bounds also removes the white space at the top you were asking about.
The issue is that UINavigationController ideally should be the direct subView of UIWindow. It will position and size right by itself. When you add UINavigationController into another custom view of a UIWindow subview, you need to take care of the position and size of this custom view by taking into account whether the status bar is shown or not in the UIWindow.
My suggestion is to make the custom view as a subclass of UINavigationController:
mySubClass_NavigationController*nav=[[mySubClass_NavigationController alloc] initWithRootViewController:viewController ]; [myUIWindow addSubview:nav.view];
and inside the mySubClass_NavigationController, you can do all the customization that you are doing now in your self (whatever that controller is).
I struggled with this for a while too using very similar code to the op’s and also had a white bar above my navigation controller.
My problem occurred when adding the UINavigationController as a view in a UITabController. The space in my case was caused by the UINavigationBar part of the UINavigationController taking into account the status bar and it was actually overlapping part of the view that I was trying to show in the UINavigationController.
This is the code I ended up with in loadView in one of my UITabBarController view controllers.
SomeUITableViewController *screenList = [[SomeUITableViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:screenList]; CGRect frame = [[navController navigationBar] frame]; frame.origin.y = 0; // Was 20, set to 0 to not take into account the status bar. [[navController navigationBar] setFrame:frame]; [self setView:[navController view]];
There’s some more information at http://discussions.apple.com/message.jspa?messageID=7890362.
There is an obscure property in IB called “Hides Bottom Bar on Push”. Just check it. It solved the problem for me.
Maybe you have somehow gotten yourself two UIViews,
each with a status bar. Check the xib.
- Proper way to connect UIActionSheet options to actions
- How to block iOS 7 control centre from controlling music app?
- Call the official *Settings* app from my app on iPhone
- Calculate distance between my location and a MapKit pin on Swift
- Objective-C forwardInvocation:
- How to get the name of enumeration value in Swift?
- Interface Builder Objects not visible in my storyboard (and titles are transparent)
- Swift 2 MKMapViewDelegate rendererForOverlay optionality
- iOS Prefix.pch best practices
- UILocalNotification not doing anything
- What are the differences between throws and rethrows in Swift?
- Migrate iCloud data to Local store and stopping iCloud from still responding
- UIKeyboardWillShowNotification issues with ios 11 beta 7
- looping through an NSMutableDictionary
- Convert basic script to Objective C (money formatting)