Is there a way to change the height of a UIToolbar?

IphoneXcodeInterface BuilderUitoolbar

Iphone Problem Overview


I've got an UIToolbar in Interface Builder and I've noticed that it's locked to being 44px tall. Of course I'd like to make this larger.

Does Apple allow resizing of this control? If so, how do I go about it?

Iphone Solutions


Solution 1 - Iphone

Sure, just set its frame differently:

[myToolbar setFrame:CGRectMake(0, 50, 320, 35)];

This will make your toolbar 35 pixels tall. Of course this requires an IBOutlet or creating the UIToolbar programmatically, but that's very easy to do.

Solution 2 - Iphone

If that does not work in SDK 6, it is possible to solve as below:

Select the toolbar element and choose Editor > Pin > Height to create a constraint. Go to your View Controller Scene and select the created Height(44) constraint, then put the value you want.

Solution 3 - Iphone

I found that if I set the frame on the iPad, when hiding/showing the Toolbar would reset itself back to a height of 44 pixels. I ended up having to override UIToolbar and change the method:

// return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize result = [super sizeThatFits:size];
    result.height = 55;
    return result;
};     

This would correct adjust the height even with the hide/show.

Solution 4 - Iphone

In iOS 6, with autolayout, the simplest approach is a UIToolbar subclass in which you override instrinsicContentSize. Here's code from one my apps, where the toolbar is tall. Its sides and bottom are pinned to the sides and bottom of the superview as usual.

-(CGSize)intrinsicContentSize {
    return CGSizeMake(UIViewNoIntrinsicMetric, 85);
}

Solution 5 - Iphone

For Xcode 7.1 iOS 9, in auto layout, the size is locked to 44px. The Xcode menu option Editor > Pin > Height is not there, instead do the following action:

In InterfaceBuilder, click the toolbar element to select it. Control+Drag down anywhere in the toolbar and release, a popup menu will display showing the option "Height" at the top, select it.

You now have a Height constraint to work with and adjust as necessary.

Solution 6 - Iphone

You could also just edit the xib file:

open it as source code and find the entry that defines the frame for the UIToolbar, something along the lines of

<string key="NSFrame">{{0,420}, {320,44}}</string>

and just change the value for 44 to whatever size you need.

This way the toolbar will be taller, and in InterfaceBuilder you'll see the new size grayed out and you'll be unable to change it, but you don't need any outlets or code.

Solution 7 - Iphone

As long as you have a height constraint on the toolbar you can use this little snippet that has helped me adjust heights for classes that inherit from UIView

-(void)setHeightConstraintTo:(CGFloat)height forView:(UIView *)view{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d",          NSLayoutAttributeHeight];
    NSArray *filteredArray = [view.constraints filteredArrayUsingPredicate:predicate];
    if(filteredArray.count > 0){
        NSLayoutConstraint *constraint = filteredArray.firstObject;
        constraint.constant = height;
    }
 }

Solution 8 - Iphone

I'm not sure how this would sit with Apple - and of course it depends how you wish to use the toolbar - but you can add a default UIView and change its class in the property inspector to UIToolbar. This gives you transparency and customisability (in this case height) for free, at the expense of the layout of bar button items.

Solution 9 - Iphone

Swift Solution:

myToolbar.frame = CGRect(x: myToolbar.frame.origin.x, y: myToolbar.frame.origin.y, width: myToolbar.frame.size.width, height: 20)

The CGRectMake is obsolete. This can be replaced with the CGRect. This will set the height of the toolbar to 20. The same works for Segmented control as well.

Solution 10 - Iphone

In interface builder, there is also the possibility to use "User Defined Runtime Attributes".

Simply add an entry with keypath set to "frame" of type "Rect" and set the value you want.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmac_55View Question on Stackoverflow
Solution 1 - IphoneDavid KanarekView Answer on Stackoverflow
Solution 2 - IphoneericmacielView Answer on Stackoverflow
Solution 3 - IphonechristophercottonView Answer on Stackoverflow
Solution 4 - IphonemattView Answer on Stackoverflow
Solution 5 - IphonecavalleydudeView Answer on Stackoverflow
Solution 6 - IphoneRadu DițăView Answer on Stackoverflow
Solution 7 - IphoneAlberto LopezView Answer on Stackoverflow
Solution 8 - IphoneRobin MachargView Answer on Stackoverflow
Solution 9 - IphoneRagul ParaniView Answer on Stackoverflow
Solution 10 - IphoneFredericKView Answer on Stackoverflow