简体   繁体   中英

UIToolbar height on device rotation

From the iOS Human Interface Guidelines, iOS UI Element Usage Guidelines

On iPhone, take into account the automatic change in toolbar height that occurs on device rotation . 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.

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 ?

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

As noted by yonel and George in the comments from 7KV7 answer, changing the autoresizing mask does not work as intended on iOS 5.

I found another solution by using the -[UIView sizeThatFits:] method. 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.

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:

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. This is what the ViewController's toolbarItems property is for, actually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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