15
votes

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?

4

4 Answers

19
votes

Did you check the auto-resizing property of the toolbar?

15
votes

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.

12
votes

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()
}
2
votes

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.