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.
- What actually is a @selector?
- Assertion failure in UITableView configureCellForDisplay:forIndexPath:
- How do I update the UI in the middle of this thread?
- How do I convert NSMutableArray to NSArray?
- Can I force a UITableView to hide the separator between empty cells?
- In iOS 8, how to add Row Action Buttons to the left side of a UITableViewCell
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.
- SKAudioNode not load sound from url
- How to 'addTarget' to UILabel in Swift
- How to open .xcodeproj programmatically?
- structure vs class in swift language
- How to display PDF (Blob) on iOS sent from my angularjs app
- Why can't you change a UIView background color in drawRect?
- NSSearchField: How to hide the icon (lupe)
- Pull to refresh not working in iOS WebView
- Display the value of a variable property in LLDB debugger?
- IOS Swift value of an Integer is not being saved
- Passing Data From iPhone app to PHP file
- Why are UIView frames made up of floats?
- Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
- Present modal view controller in half size parent controller
- Pulling data from a CMSampleBuffer in order to create a deep copy