iOS Autolayout and UIToolbar/UIBarButtonItems

outerstorm picture outerstorm · Feb 20, 2013 · Viewed 21.2k times · Source

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?

Answer

snorock picture snorock · Mar 27, 2013

Autolayout constraints only work with UIViews and their subclasses.

While UIToolbar allows some UIView based items (such as UISearchBar and UISegmentedControl) they may have to coexist with UIBarButtonItems which do not inherit from UIView.

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.