我的应用程序的根视图控制器支持任何方向,但用户可以导航到另一个仅支持横向方向的视图控制器。
基本问题是,无论设备的实际物理方向如何,当用户弹回根视图控制器时,导航栏的高度都适合横向模式。我想弄清楚如何让导航栏显示高度为44(纵向高度)而不是32(横向高度)。
虽然导航栏未设置为正确的高度,但根视图控制器的UITableView正确偏移(距屏幕顶部44个像素)。
在第二个视图控制器的-viewWillAppear中:我执行标准旋转变换:
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];
}
因为这个辅助视图控制器允许导航到从属视图控制器(以及弹回到根视图控制器),所以我将以下内容添加到-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 = CGAffineTransformIdentity;
navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
感谢您提供有关如何强制重新计算导航栏高度的建议。
答案 0 :(得分:8)
不确定是否有更好的方法,但这有效:
[[self navigationController] setNavigationBarHidden:YES animated:NO];
[[self navigationController] popViewControllerAnimated:YES];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
基本上,隐藏导航栏,弹出视图控制器并显示导航栏。某些代码可以调整导航栏的高度,一切都很好。