UINavigationController within ViewController, gap

2019-03-12 06:49发布

I'm working on a universal app, and I'm trying to share as much code as possible between the iPhone and iPad versions. I need to use a TabBarController as my root view controller and though I'd like to use a SplitViewController in each of the tabs, SplitViewController's docs say it needs to be the root view controller. So, with all of that in mind - understand that I'm attempting to place two navigation controllers side-by-side and (mostly) replicate the behavior/layout of a SplitViewController.

Everything works just fine, except for the layout of the views. When the app is started in portrait mode, everything functions and resizes/positions correctly when the device orientation changes.

portrait orientation

If the app is started in any orientation other than UIDeviceOrientationPortrait, the view displays with a 20 point gap/margin above the navigation controller. I've tried adjusting the frame at runtime with no perfect result. Adjusting the origin.y of the frame to -20 and increasing the height by 20 brings the view flush with the top of it's parent, but it leaves a 20 point gap at the bottom!

landscape orientation

11条回答
Viruses.
2楼-- · 2019-03-12 07:13

The proper answer is:

CGRect r = [[_navController view] frame];
r.origin = CGPointMake(0.0f, -20.0f);
[[_navController view] setFrame:r];
查看更多
我只想做你的唯一
3楼-- · 2019-03-12 07:14

To fix this problem just check the box Wants Full Screen on the storyboard.

The problem appears because the ParentViewController is showing the navigation bar.

As apple documentation said :

If your app displays the status bar, the view shrinks so that it does not underlap the status bar. After all, if the status bar is opaque, there is no way to see or interact with the content lying underneath it. However, if your app displays a translucent status bar, you can set the value of your view controller’s wantsFullScreenLayout property to YES to allow your view to be displayed full screen. The status bar is drawn over the top of the view.

查看更多
男人必须洒脱
4楼-- · 2019-03-12 07:17

I ran into the same issue when adding a UITableView to my ViewController on viewDidLoad. Instead of grabbing the frame from self.view, I got it form the main window so that it looks like this

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
self.uiTable = [[UITableView alloc] initWithFrame:[keyWindow frame] style:UITableViewStylePlain];
查看更多
Fickle 薄情
5楼-- · 2019-03-12 07:17

In my view that is displayed inside the navigationcontroller I put this code inside the viewDidAppear

This code may not be perfect for every application but it fixed my issue I spent several hours kicking at..

    // START OF BUG FIX FOR iOS
if (self.navigationController.navigationBar.frame.origin.y ==20) {
    // Move the navigation Bar up
    [self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
    // move up the table view
     [self.view setFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height+20)];
}
// END OF BUG FIX for IOS6
查看更多
Viruses.
6楼-- · 2019-03-12 07:21

Oddly enough, what's helping for me in iOS 6 is subclassing UINavigationController and implementing this method:

- (BOOL)wantsFullScreenLayout {
    return NO;
}

Unchecking "Wants Full Screen" in my storyboard didn't do the trick, so I don't know what's different. This could certainly break in a future version of iOS. But for now, I'll take it.

查看更多
登录 后发表回答