60

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?

Constantino Tsarouhas
  • 6,736
  • 6
  • 43
  • 54
mac_55
  • 3,660
  • 7
  • 27
  • 40

10 Answers10

65

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.

cdespinosa
  • 19,973
  • 6
  • 32
  • 39
David Kanarek
  • 12,439
  • 5
  • 43
  • 61
46

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.

ericmaciel
  • 469
  • 4
  • 2
38

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.

christophercotton
  • 5,699
  • 1
  • 32
  • 48
  • 1
    I found that when I have an odd value for height (in non-retina resolution), the origin is shifted by 0.5 pixels. I had to correct navbar and toolbar's frames in viewDidLoad method of VCs – Ege Akpinar Nov 28 '12 at 18:16
  • @Ege, can you elaborate more? – Mark A. Donohoe Jan 20 '13 at 10:16
  • 1
    Sure @MarqueIV, I override sizeThatFits for custom height of a Navigation bar (not toolbar) and found out that when I have a height that is not a multiple of 4 points, when I hide the navigation bar and re-display it, its origin's y value is 0.5 instead of 0. I do not experience this behaviour when I have a custom height value that is a multiple of 4 – Ege Akpinar Jan 20 '13 at 13:27
  • @Ege What? You cannot set the points value directly. Unless you use .5 increments.. – Maciej Swic Sep 26 '13 at 06:07
  • @MaciejSwic I don't follow what you're trying to say. y value becomes 0.5, I don't set it automatically – Ege Akpinar Sep 26 '13 at 22:25
  • @EgeAkpinar What did you end up doing about this 0.5 discrepancy? – SAHM Apr 26 '16 at 02:25
  • 1
    If you want to change toolbar height as the device rotates (as is iOS default now), you can add something like: `result.height = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? 55 : 40;` – Nate Apr 06 '17 at 02:04
  • This seems to be the only method that also works for iOS 12, where toolbars have a larger default height on the iPad. I use it to set the default height back to 44.0. – Ely Sep 22 '18 at 13:06
16

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);
}
matt
  • 447,615
  • 74
  • 748
  • 977
  • I like your solution. I'm using the built in toolbar on UINavigationController, so I can't subclass. Instead I swizzled `intrinsicContentSize` on `UIToolbar` and returned my desired height. – Kyle Redfearn Feb 26 '16 at 22:20
  • @KyleRedfearn "I'm using the built in toolbar on UINavigationController, so I can't subclass" Not true. `initWithNavigationBarClass:toolbarClass:` lets you use a UIToolbar subclass with your navigation controller. – matt Feb 26 '16 at 22:22
  • Oh yeah. I had forgotten about the initializer. Thanks! – Kyle Redfearn Feb 27 '16 at 02:54
14

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.

cavalleydude
  • 149
  • 1
  • 5
7

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.

Radu Diță
  • 8,910
  • 1
  • 18
  • 27
  • This does not seem to work any more. Tested this just now on XCode 5.1.1 for a storyboard using iOS7 styling. The values are being reset to their defaults. – Martin May 11 '14 at 21:12
  • 1
    actually works for ios10 swift and the line you need to look for is `` – Lamour Aug 02 '16 at 16:34
  • This worked spot on for me using a custom Xib, note for IOS13 height can now be found in this line: `` – GordonW Apr 29 '20 at 20:57
2

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;
    }
 }
Alberto Lopez
  • 237
  • 4
  • 10
1

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.

Robin Macharg
  • 699
  • 7
  • 15
1

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.

Ragul Parani
  • 543
  • 6
  • 19
0

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

FredericK
  • 1,440
  • 1
  • 17
  • 24