设备旋转时的UIToolbar高度

0xc*_*ced 15 iphone uitoolbar ios

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

在iPhone上,考虑设备旋转时工具栏高度的自动更改.特别是,请确保您的自定义工具栏图标非常适合横向显示的细条.不要以编程方式指定工具栏的高度.

我可以看到Mail,Twitter for iPhoneDropbox的高度从44点变为32点,但是当我添加工具栏(使用Interface Builder)并让我的UIViewController子类自动旋转(shouldAutorotateToInterfaceOrientation:返回YES)时,工具栏会不会在设备旋转时自动更改其高度.

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

vis*_*kh7 19

您是否检查了工具栏的自动调整大小属性?

  • 就是这样,你必须要做`toolbar.autoresizingMask = toolbar.autoresizingMask | UIViewAutoresizingFlexibleHeight;`以编程方式作为Interface Builder不允许您更改灵活的高度! (11认同)
  • 使用此解决方案,iOS5.0不会产生副作用吗?它工作正常,但工具栏中的图标始终具有横向(=较小)尺寸,即使在纵向:/ (2认同)

0xc*_*ced 15

正如yonelGeorge7KV7回答的评论中指出的那样,更改自动调整遮罩在iOS 5上无法正常工作.

我通过使用- [UIView sizeThatFits:]方法找到了另一种解决方案.我是这样做的:

- (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))};
}
Run Code Online (Sandbox Code Playgroud)

然后我layoutForInterfaceOrientation:在视图控制器viewWillAppear:willAnimateRotationToInterfaceOrientation:duration:方法中调用此方法.

尽管如此,高度仍以编程方式更改,但至少该值不是硬编码的.


Bri*_*kel 12

使用自动布局实现此行为要简单得多.我只在iOS8中进行过测试,但以下代码适用于方向更改所需的动画:

public override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    toolbar.invalidateIntrinsicContentSize()
}
Run Code Online (Sandbox Code Playgroud)