设备旋转时的UIToolbar高度

时间:2011-03-07 12:40:22

标签: iphone ios uitoolbar

来自iOS人机界面指南,iOS UI Element Usage Guidelines

  

在iPhone上,请考虑到   自动更改工具栏高度   在设备轮换时发生。在   特别是,请确保您的自定义   工具栏图标非常适合更薄   出现在风景中的酒吧   取向。不要指定高度   以编程方式显示工具栏。

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

UIToolbar Class Reference没有提到这种高度的自动更改,所以我应该以编程方式更改它,即使HIG说不要以编程方式指定工具栏的高度? / p>

4 个答案:

答案 0 :(得分:19)

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

答案 1 :(得分:15)

正如 7KV7 答案中的 yonel George 所述,更改自动调整大小屏幕在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))};
}

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

尽管如此,高度仍以编程方式更改,但至少该值未经过硬编码。

答案 2 :(得分:12)

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

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

答案 3 :(得分:2)

如果在NavigationController中使用ViewController,则可以使用NavigationController的工具栏而不是创建自己的工具栏,并让它处理调整大小。这就是ViewController的toolbarItems属性,实际上是。

相关问题