Search code examples
objective-ciosxcodeipaduitoolbar

Objective C: Adaptive Toolbar On Orientation Change


I am having a problem with my toolbar when i change the orientation of my iPad.

i set my nib file into landscape and everything is all right but when i turned it to portrait my toolbar still has the width from the landscape orientation.

how will i make my toolbar adaptive to the orientation change to portrait?

Landscape: enter image description here

Portrait:

enter image description here

thanks!


Solution

  • Try adding UIViewAutoresizingFlexibleWidth to the toolbar autoresizingMask like so:

    myToolbar.autoresizingMask |= UIViewAutoresizingFlexibleWidth
    

    Or, if your doing this in the Interface Builder, make sure this horizontal bar is selected (others may be selected as well, which is fine):

    enter image description here

    More from the UIView Class Reference about autoresizingMask:

    When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all.

    When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view’s left margin is fixed but that the view’s width and right margin may change. Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase.

    If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely.