I have an iOS view with autolayout enabled and have a UIToolbar
with a UISearchBar
and UISegmentControl
contained with the toolbar. I want the UISearchBar
to have a flexible width so I need to add a constraint to force this, but from what I can tell you cannot add constraints to items in a UIToolbar
in Interface Builder. The options are all disabled.
Before AutoLayout
I would accomplish this with autoresizingmasks
.
Are constraints not allowed within UIToolbars/UINavigationBars
?
How else can this be accomplished when using autolayout?
This can also be done right from a storyboard.
Just drag and drop items in the toolbar, and turn some of them into flexible or fixed space to get the desired effect. See the two examples below.
NB: this is a copy of my answer to Aligning UIToolBar items, I stumbbled upon both questions while looking for such a solution
Autolayout constraints only work with
UIViews
and their subclasses.While
UIToolbar
allows someUIView
based items (such asUISearchBar
andUISegmentedControl
) they may have to coexist withUIBarButtonItems
which do not inherit fromUIView
.Until autolayout can work with
UIBarButtonItems
, do as you have done.Your alternative is to roll your own toolbar with widgets based only on
UIViews
.You can do this in code, at least; I'm the type to forsake Interface Builder and go it in code anyway. IB seems to get in my way more often than not when it comes to adding or tweaking constraints. Here's what I've done in my custom
UIToolbar
subclass's-initWithFrame:
method.And since I like to lazy load as much as possible, here's my
self.label
instance variable (called when[self addSubview:self.label]
gets messaged above).Seems to work for me. I'm not adding any
UIBarButtonItems
, though, so your mileage my vary.