开发者

iPhone navigation bar height remains short in portrait orientation

开发者 https://www.devze.com 2023-01-08 19:07 出处:网络
My app\'s root view controller supports any orientation but user can navigate to another view controller that supports only landscape orientation.

My app's root view controller supports any orientation but user can navigate to another view controller that supports only landscape orientation.

The basic problem is that regardless of the device's actual physical orientation, when the user pops back to root view controller, the navigation bar has a height that would be appropriate for landscape mode. I would like to figure out how to get navigation bar to appear with height of 44 (portrait height) instead of 32 (landscape height).

Although the navigation bar is not set to proper height, the root view controller's UITableView is offset correctly (44 pixels from top of screen).

Inside the second view controller's -viewWillAppear: I perform the standard rotational transform:

if (deviceOrientation == UIDeviceOrientationPortrait)
   {
   UIScreen *screen = [UIScreen mainScreen];
   CGFloat screenWidth = screen.bounds.size.width;
   CGFloat screenHeight = screen.bounds.size.height;
   UIView *navView = [[self navigationController] view];
   navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
   navView.transform = CGAffineTransformIdentity;
   navView.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
   navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
   }

Because this secondary view controller allows navigation to subordinate view controllers (as well as popping back up to root view controller), I have added the following to -viewWillDisappear:

if (deviceOrientation == UIDeviceOrientationPortrait)
   {
   UIScreen *screen = [UIScreen mainScreen];
   CGFloat screenWidth = screen.bounds.size.width;
   CGFloat screenHeight = screen.bounds.size.height;
   UIView *navView = [[self navigationController] view];
   navView.bounds = CGRectMake(0, 0, screenWidth, screenHeight);
   navView.transform = CGAffine开发者_Go百科TransformIdentity;
   navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
   }

Thanks for any advice on how to force recalculation of navigation bar height.


Not sure if there's a better way, but this worked:

[[self navigationController] setNavigationBarHidden:YES animated:NO];
[[self navigationController] popViewControllerAnimated:YES];
[[self navigationController] setNavigationBarHidden:NO animated:NO];

Basically, hide the nav bar, pop the view controller and the show the nav bar. Some code somewhere adjusts the nav bar height and everything's fine.

0

精彩评论

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

关注公众号