开发者

Autoresizing failure in a modal NavigationController

I've searched Stackoverflow and googled till I'm blue in the face and still can't work this out...

I have a not-too complicated app: a tab bar controller, with one of the root view controllers for a tab being a UINavigationController. A few regular VCs I've written get pushed onto this nav controller in the usual way. One of those VCs has a UIButton which presents a modal dialog and that is where I'm having problems.

Here's a picture to help illustrate:

UITabBarController
  \---- Tab1 root VC: UINavigationController
       \---- Nav root VC: UIViewController
    开发者_StackOverflow社区        \---- ...onto which we push: UIViewController
                 \---- ... on which we present modally: UINavigationController
                      \---- Nav root VC: UIViewController

The modal dialog is a UINavigationController and for the rootViewController I have a simple VC that presents a single UIView in it. My VC supports landscape rotation (which the rest of the app doesn't, hence the strategy of presenting it as a modal).

The problem: upon rotation, and upon subsequent rotations, the autoresize behaviour is doing something odd for that UIView: the top of the UIView ends up right against the status bar, i.e. underneath the navigation bar. The left, right, bottom edges are in the correct place.

I've checked and double checked - all my autoresizeMasks look correct, the autoresizesSubviews flags look correct, there's no weird insets set anywhere, my UINavigationController isn't doing anything funky like setting the navbar style to translucent.

I've recreated the situation in a new test app, but omitting the UITabBarController at the top of the heirarchy, and everything behaves as expected.


One of the important points about UITabBarController is that it can support autorotation only if all of its views do as well. This S.O. answer may help finding a way out.

On another note, the height of the status bar for a UINavigationController changes between the two modes, so you'll have to adjust for it:

 CGRect frame = self.navViewController.navigationBar.frame;
 if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 44;
 } else {
    frame.size.height = 32;
 }
 self.navViewController.navigationBar.frame = frame;

EDIT: If that does not work, you can try and force the change of device orientation programmatically like this:

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];

possibly this will make your main window resize the navigation bar.


Fixed!

Here's what the issue was:

I am using MBProgressHUD to show a progress spinner over the view in my modal. So in viewDidLoad I had:

[self.navigationController.view addSubview:progressHUD];

The addition of this HUD component to that view was screwing up the resizing upon rotation; if I instead do the following things work again:

[self.view addSubview:progressHUD];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜