典型的 NavController 行为是在港口/景观中调整大小。从 44px 高度到 32px 我相信(从我的头顶)。
我正在使用 Storyboard并将导航栏包含在 VC 中,而不是 Controller 中。在纵向和横向之间旋转时,导航栏不会自动调整大小。
我看过这个答案,但这没有帮助:Resizing UINavigationBar on rotation
导航栏是通过 App Delegate 加载的外观 Controller :
UIImage *portraitImage = [UIImage imageNamed:@"mainNavBar"];
UIImage *landscapeImage = [UIImage imageNamed:@"mainNavBarLandscape"];
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,nil]];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];
不幸的是,最后一部分没有任何区别,因为导航栏将包含在多个 Controller 中,我不想在上面的问题中重复相同的胶水代码,以在旋转时调整大小。
最佳答案
如果您使用自动布局,UINavigationBar
的 intrinsicContentSize
会在旋转时正确更新,但其高度 NSContentSizeLayoutConstraint
不会。手动调用 invalidateIntrinsicContentSize
告诉自动布局更新导航栏的高度限制。
// View controller
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransition(to: size, with: coordinator);
self.navigationBar?.invalidateIntrinsicContentSize();
}
关于ios - 旋转到横向时自动调整 UINavigationBar 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16304881/