开发者

Troubles with iPhone UINavigationController (UINavigationBar in wrong place)

开发者 https://www.devze.com 2023-03-06 20:44 出处:网络
I\'m in the process of making some adjustments to an app, including changing to a navigation-based interface. As part of that change I\'ve written a view controller that contains a UINavigationControl

I'm in the process of making some adjustments to an app, including changing to a navigation-based interface. As part of that change I've written a view controller that contains a UINavigationController. The problem is, for some strange reason the UINavigationBar and UIToolbar managed by the UINavigationController are displaced 20px down from where they should be. I've managed to produce the following example that demonstrates the issue:

// MyAppDelegate.m

@implementation MyAppDelegate
@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.frame = [[UIScreen mainScreen] applicationFrame];
    [self.window makeKeyAndVisible];

    TestController* tc = [TestController new];
    [self.window addSubview:tc.view];

    return YES;
}

@end

// TestController.m

@implementation TestController

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectZero];

    UINavigationController* navController = [UINavigationController new];
    navController.view.backgroundColor = [UIColor blueColor];
    [navController setToolbarHidden:NO animated:NO];

    [self.view addSubview:navController.view];
}

@end

This produces the following result on my machine:

Troubles with iPhone UINavigationController (UINavigationBar in wrong place)

As you can see, the controls are 20px down from where I'd expect them to be. I've tried just about everything I can think of (various combinations of wantsFullScreenLayout, au开发者_开发知识库toresizesSubviews, etc) with no positive effect. This also has nothing to do with programatically messing with the statusbar (as seems to be the case in most other examples of this I have come across), since I do not at any point mess with the statusbar. This occurs with or without a root view controller in the navigation controller - if there is one, it's contents are shifted 20px down too (so they actually are in the right place relative to the navigation bar and toolbar).

Any help much appreciated!

EDIT: After a bit of investigation, it seems that removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to correct the positioning of the navigation bar and toolbar and content. That said, now some other views in the application are in the wrong place (up underneath the statusbar). My understanding is that line is generally recommended to ensure that the window is the correct size?


As mentioned in my edit, removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to have corrected 95% of my problems. I've managed to fudge an approach to fix the other 5% by using the same background colour for my window and the remaining views having issues, but I can't say I'm thrilled with this solution - I shouldn't have to do that.

I'll keep experimenting, and if I find a better result will certainly post an edit here.


UINavigationController does not play nicely with being used as a subview; as you've noticed, it will often leave room for the status bar even when it is not actually under the status bar. If you're not trying to write your own container view controller, you should rework your code to not be adding a view controller's view as a subview at all.

That said, I've had luck fixing it by setting wantsFullScreenLayout to NO on the UINavigationController, which will make it not leave space for the status bar. You would, of course, want to do this just after allocating it, before loadView gets triggered.

0

精彩评论

暂无评论...
验证码 换一张
取 消