Why doesn't my root view's height take acc

2019-02-17 18:21发布

I was under the impression that a UINavigationController's navigation bar would always push down the child view's height, such that the child view's origin was at the bottom of the title bar.

But when I present a view controller like this ...

MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
viewController.title = @"My View Controller";
viewController.navigationItem.prompt = @"My Prompt";
viewController.delegate = self;

[self presentModalViewController:navigationController animated:YES];
[navigationController release]; [viewController release];

... and then check self.view.frame.size.height with an NSLog in viewDidLoad, it reports that my view is 460px high. Shouldn't it subtracting the height of my title & prompt?

2条回答
欢心
2楼-- · 2019-02-17 18:51

Probably you are referring to the wrong self here. When self refers to the MyViewController instance you will probably have an height of 460 - 44 pixels (standard UIToolbar height), but if self refers to a parent view controller (e.g. the navigation view controller itself) you'll see the standard view height (e.g. 460px).

You can check this by calling self.view.frame.size.height inside the instance of MyViewController, e.g. when the view has finished loading (viewWillAppear: method), and so has already been resized by its navigation controller.

If you call self.view.frame.size.height from the viewDidLoad method, the view probably has still not been resized by its parent controller.

Please try again and let me know if this fixes your problem.

查看更多
何必那么认真
3楼-- · 2019-02-17 18:55

So, as requested:

When you call viewDidLoad the view controller hasn't been pushed onto the screen yet. So when you get the frame size from within that method it will report its' default (typically, 320x480 for an iPhone app).

The view then autoresizes to take into account the navigation bar. So when you check the frame size in viewWillAppear it will now be correct. Typically this isn't a problem for iPhone apps. For iPad apps, where you have multiple orientations, it can be a bit of a pain!

There are a few exceptions to this - for example, when using NIBs.

查看更多
登录 后发表回答