Is there a way to change the height of a UIToolbar?
Asked Answered
O

10

61

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?

Orthopedics answered 25/1, 2010 at 20:31 Comment(0)
T
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.

Tithing answered 26/1, 2010 at 0:8 Comment(4)
Will resizing UIToolbar be rejected from AppStore?Smallclothes
Nope, I tried and got trough. But sometimes it is up to the daily mood of your apple guy.Brasilein
I don't like this solution because it causes the tool bar to be resized on appearance transitions. The sizeThatFits solution below works bestDemit
Does not work on latest iOS, answer is completely wrongNarrate
M
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.

Mussman answered 12/4, 2013 at 21:21 Comment(3)
I am so glad I found this comment, I wish I could up vote you a thousand times more. Spent wayyyyy to much time trying to figure this out in code.Sisneros
xcode 8 has this capability. Not sure what happened between the diff versions.Ludewig
Where is it located now in later versions? I don't see it under editorBeaverette
M
39

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.

Moxley answered 18/5, 2011 at 19:12 Comment(8)
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 VCsLibbielibbna
@Ege, can you elaborate more?Lanita
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 4Libbielibbna
@Ege What? You cannot set the points value directly. Unless you use .5 increments..Caulicle
@MaciejSwic I don't follow what you're trying to say. y value becomes 0.5, I don't set it automaticallyLibbielibbna
@EgeAkpinar What did you end up doing about this 0.5 discrepancy?Gandzha
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;Lurcher
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.Terbia
C
17

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);
}
Colquitt answered 25/4, 2013 at 1:31 Comment(3)
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.Demit
@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.Colquitt
Oh yeah. I had forgotten about the initializer. Thanks!Demit
H
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.

Hydride answered 7/11, 2015 at 1:7 Comment(3)
Great, Clean, Simple.Fallible
I don't think this works with a toolbar in a UINavigationController.Gandzha
Though I was able to change the height using this method, it affected the way the toolbar appeared on the view. For a fraction of second, it appeared few pixels above it's targeted location.Breaux
P
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.

Propolis answered 25/4, 2013 at 18:29 Comment(3)
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.Hargrave
actually works for ios10 swift and the line you need to look for is <constraint firstAttribute="height" constant="80" id="fZW-ej-FYO"/>Araroba
This worked spot on for me using a custom Xib, note for IOS13 height can now be found in this line: <rect key="frame" x="0.0" y="0.0" width="187" height="57"/>Sooty
P
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;
    }
 }
Postulant answered 23/9, 2014 at 19:49 Comment(0)
V
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.

Vesuvius answered 18/10, 2013 at 9:4 Comment(0)
V
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.

Voorhees answered 29/8, 2017 at 8:53 Comment(0)
G
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

Glycoside answered 31/12, 2017 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.