繁体   English   中英

设备旋转时的UIToolbar高度

[英]UIToolbar height on device rotation

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

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

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

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

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

正如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))};
}

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM