Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Autolayout and UIToolbar/UIBarButtonItems

Tags:

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?

like image 828
outerstorm Avatar asked Feb 20 '13 14:02

outerstorm


People also ask

What is IOS 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.

What is the use of Autolayout?

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.

What are IOS constraints?

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.


1 Answers

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.

like image 145
snorock Avatar answered Sep 18 '22 11:09

snorock