Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 919
mac_55 Avatar asked Jan 25 '10 20:01

mac_55


9 Answers

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.

like image 54
David Kanarek Avatar answered Sep 21 '22 18:09

David Kanarek


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.

like image 44
ericmaciel Avatar answered Sep 21 '22 18:09

ericmaciel


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.

like image 27
christophercotton Avatar answered Sep 18 '22 18:09

christophercotton


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);
}
like image 34
matt Avatar answered Sep 18 '22 18:09

matt


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.

like image 38
cavalleydude Avatar answered Sep 17 '22 18:09

cavalleydude


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.

like image 43
Radu Diță Avatar answered Sep 20 '22 18:09

Radu Diță


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;
    }
 }
like image 42
Alberto Lopez Avatar answered Sep 19 '22 18:09

Alberto Lopez


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.

like image 25
Robin Macharg Avatar answered Sep 18 '22 18:09

Robin Macharg


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.

like image 41
Ragul Parani Avatar answered Sep 21 '22 18:09

Ragul Parani