Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of UIView's autoresizingMask?

Tags:

After reading about UIView's autoresizingMask on SO and developer.apple.com I'm still unclear what the purpose is. What's a situation where setting this property is necessary?

like image 782
SundayMonday Avatar asked Sep 07 '11 16:09

SundayMonday


People also ask

How does AutoresizingMask work?

AutoresizingMask 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 none , which indicates that the view should not be resized at all.

What is AutoresizingMask in IOS?

The options that determine how the view is resized relative to its superview. macOS 10.0+

What is translatesAutoresizingMaskIntoConstraints in Swift?

translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.

What does Documentation call the parent view that's embedding the other view?

Embedding one view inside another creates a containment relationship between the host view (known as the superview) and the embedded view (known as the subview).


1 Answers

Yes, it is often necessary to set it if you don't want to resize the views manually. Note that it is mostly useful for subviews (i.e. those views that don't take the whole screen) rather then the main view of your app.

Views typically may need resizing if:

  • the device is rotated
  • an extra view (say, an ad) is added to the view, so the existing subviews have less available space.

For example, suppose if you have a view with two buttons on it, one in the top-left corner, another in the top-right corner. In order for the buttons to get wider when the view transitions from portrait to landscape, you need to set the FlexibleLeftMargin to the right button, FlexibleRightMargin to the left button.

Edit: autoresizingMask is also the first thing to look at if you see weird holes or overlaps when device is rotated or a new subview is added. Quite often the proper setting of these masks for subviews can get you a nice looking view in both orientations without having to lay out subviews manually - but usually it takes some experimenting.

Edit2: (since this is still gathering upvotes) Autoresizing masks are now mostly superseded with "Auto Layout", which allows for much more flexible constraints on views' sizes and positions. That being said, translatesAutoresizingMaskIntoConstraints is still occasionally useful for dynamically added views.

like image 160
SVD Avatar answered Oct 04 '22 22:10

SVD