设备旋转时的UIToolbar高度

从iOS人机界面指南, iOS UI元素使用指南

在iPhone上,请考虑设备旋转时自动更改工具栏高度 。 尤其要确保您的自定义工具栏图标适合横向出现的更细条。 不要以编程方式指定工具栏的高度。

我可以看到邮件iPhoneDropbox的高度从44点到32点,但当我添加一个工具栏(与界面生成器),并有我的UIViewController子类自动旋转( shouldAutorotateToInterfaceOrientation:返回YES),工具栏不会自动更改设备旋转时的高度。

UIToolbar类参考没有提到这个高度的自动改变,所以我应该改变它编程,即使HIG说不要以编程方式指定工具栏的高度

你检查了工具栏的自动resize的属性吗?

正如YonelGeorge7KV7的回答中指出的那样 ,更改自动调整掩码在iOS 5上不能正常工作。

我通过使用- [UIView sizeThatFits:]方法find了另一个解决scheme。 我是这样做的:

 - (void) layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Adjust the toolbar height depending on the screen orientation CGSize toolbarSize = [self.toolbar sizeThatFits:self.view.bounds.size]; self.toolbar.frame = (CGRect){CGPointMake(0.f, CGRectGetHeight(self.view.bounds) - toolbarSize.height), toolbarSize}; self.webView.frame = (CGRect){CGPointZero, CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetMinY(self.toolbar.frame))}; } 

然后我在我的视图控制器viewWillAppear:willAnimateRotationToInterfaceOrientation:duration:方法中调用这个layoutForInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration:方法。

尽pipe如此,这个高度仍然是以编程方式改变的,但是至less这个值并不是硬编码的。

使用自动布局来实现这种行为要简单得多。 我只在iOS8中testing过,但是下面的代码适用于我所期望的方向变化animation:

 public override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() toolbar.invalidateIntrinsicContentSize() } 

如果您在NavigationController中使用ViewController,则可以使用NavigationController的工具栏而不是创build自己的,并让其处理resize。 这是ViewController的toolbarItems属性实际上的作用。