简体   繁体   English

设备旋转时的UIToolbar高度

[英]UIToolbar height on device rotation

From the iOS Human Interface Guidelines, iOS UI Element Usage Guidelines 来自iOS人机界面指南, iOS UI元素使用指南

On iPhone, take into account the automatic change in toolbar height that occurs on device rotation . 在iPhone上,考虑设备旋转时工具栏高度的自动更改 In particular, make sure your custom toolbar icons fit well in the thinner bar that appears in landscape orientation. 特别是,请确保您的自定义工具栏图标非常适合横向显示的细条。 Don't specify the height of a toolbar programmatically. 不要以编程方式指定工具栏的高度。

I can see the height changing from 44 points to 32 points in Mail , Twitter for iPhone and Dropbox for example, but when I add a toolbar (with Interface Builder) and have my UIViewController subclass to automatically rotate ( shouldAutorotateToInterfaceOrientation: returns YES), the toolbar does not automatically change its height on device rotation. 我可以看到MailTwitter for iPhoneDropbox的高度从44点变为32点,但是当我添加一个工具栏(使用Interface Builder)并让我的UIViewController子类自动旋转( shouldAutorotateToInterfaceOrientation:返回YES)时,工具栏不会在设备旋转时自动更改其高度。

The UIToolbar Class Reference does not mention this automatic change of height, so am I supposed to change it programmatically even though the HIG says Don't specify the height of a toolbar programmatically ? UIToolbar类参考没有提到这种高度的自动更改,所以我应该以编程方式更改它,即使HIG说不要以编程方式指定工具栏的高度

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

As noted by yonel and George in the comments from 7KV7 answer, changing the autoresizing mask does not work as intended on iOS 5. 正如yonelGeorge7KV7回答的评论中指出的那样 ,更改自动调整遮罩在iOS 5上无法正常工作。

I found another solution by using the -[UIView sizeThatFits:] method. 我通过使用- [UIView sizeThatFits:]方法找到了另一种解决方案。 Here is how I did it: 我是这样做的:

- (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))};
}

Then I call this layoutForInterfaceOrientation: method in my view controller viewWillAppear: and willAnimateRotationToInterfaceOrientation:duration: methods. 然后我在视图控制器viewWillAppear:willAnimateRotationToInterfaceOrientation:duration: methods中调用这个layoutForInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration:方法。

The height is nonetheless changed programmatically but at least the value is not hardcoded. 尽管如此,高度仍以编程方式更改,但至少该值不是硬编码的。

This behavior is a lot simpler to achieve with auto layout. 使用自动布局实现此行为要简单得多。 I've only tested in iOS8 but the following code works for me with the desired animation on orientation change: 我只在iOS8中进行过测试,但以下代码适用于方向更改所需的动画:

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

If you use a ViewController inside a NavigationController, you can use the NavigationController's toolbar instead of creating your own, and let it handle the resizing. 如果在NavigationController中使用ViewController,则可以使用NavigationController的工具栏而不是创建自己的工具栏,并让它处理调整大小。 This is what the ViewController's toolbarItems property is for, actually. 实际上,这就是ViewController的toolbarItems属性的toolbarItems

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

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