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?
Auto Layout is a constraint-based layout system. It allows developers to create an adaptive interface that responds appropriately to changes in screen size and device orientation.
Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.
Together, an interface's constraints describe the ways views can be laid out to dynamically fit any screen or window geometry. In Cocoa and Cocoa Touch, a well-defined interface layout consists of constraints that are satisfiable and sufficient. Each individual constraint refers to either one or two views.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With