How to Use Uiview Autoresizingmask Property Programmatically

How to use UIView autoresizingMask property programmatically?

When setting the autoresizing mask for a view, use a bitwise inclusive OR (|) (Objective-C), or an array (Swift 2, 3, 4) to specify springs and struts.

  • Springs are represented by specifying a mask (Objective-C or Swift, respectively):

    • vertical spring: UIViewAutoresizingFlexibleHeight or .flexibleHeight

    • horizontal spring: UIViewAutoresizingFlexibleWidth or .flexibleWidth

  • Struts are represented by the lack of one of the four 'flexible margin' masks (i.e. if a strut does not exist, the mask for that margin is specified):

    • UIViewAutoresizingFlexibleLeftMargin or .flexibleLeftMargin

    • UIViewAutoresizingFlexibleRightMargin or .flexibleRightMargin

    • UIViewAutoresizingFlexibleTopMargin or .flexibleTopMargin

    • UIViewAutoresizingFlexibleBottomMargin or .flexibleBottomMargin

For example, a view with a horizontal spring and top and bottom struts would have the width, and left and right margins specified as flexible:

Swift 3 and 4

mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]

Swift 2

mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]

Swift 1.2

mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin

Objective-C

mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth |    
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin);

Sample Image

Set UIView's autoresizing mask programmatically?

To achieve what you have in that screen shot you need to do the opposite of what DrummerB suggests. You want a fixed top margin so you make every other side flexible like so:

Objective C:

view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;

Not setting a side as flexible means that it will be fixed (default behaviour), thats why there is no such thing as UIViewAutoResizingFixedTopMargin (since its the same as not setting UIViewAutoresizingFlexibleTopMargin)

Edit for Swift:

view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin]

Credit to Tom Calmon for adding the swift version 1st.

Swift 5.0 update:

view.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]

Cheers.

Autoresizing Not Working Programmatically

Little mistake from my side.
My code is correct.

The issue is related to xCode 6.1, in launch screen we have xib file from xCode 6 so i have to remove reference of it from General Setting-> App Icon and Launch Images and use Assert Catalog instead of launchscreen.xib and it start working....

Anyway thanks for reply to my question.

What is the purpose of UIView's autoresizingMask?

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.

UIView and AutoresizingMask ignored

Well, sure enough, it was my own fault.

Lesson number one: You are not doing yourself any favors by staying up all night trying to fix broken code. Get some rest! Drink more water! You will only probably make things worse if you keep trying to force your brain to perform complicated algorithmic strategy and logic past its bed-time.

Turns out I had a rogue UIView toward the bottom of the view hierarchy that did not have the autoresize property set at all. I thought I had looked through everything, but turns out that I missed one. (Just one little view, and an entire day shot!)

I can say for anyone who comes along later with similar frustration, that Autoresizing does indeed work as documented. If you think that "something is not being called" then you are probably looking in the wrong place. Also, the UIViewAutoresizingMask enum constants are not used exactly like they are in Interface Builder. In IB, you "lock" margins, whereas in setting them programmatically, the locked margins are assumed by default and you "unlock" them by setting it as "Flexible". So for example, setting only the FlexibleWidth and FlexibleHeight bits is equivalent to enabling all autoresizing options in IB. By throwing in any of the margin masks (i.e. UIViewAutoresizingFlexibleLeftMargin) you are "deselecting" the corresponding margin autoresizing option in IB. (I've seen many other posts floating around where this seemed to be a major point of confusion for some folks.)

During my research, however, I did notice that there does not seem to be any sort of event, message, notification, or otherwise when a UIView gets resized, whether automatically or not. While not necessary for resizing subViews, it would be nice to know when it's happening in the situation where you would like to cause a scrollView or tableView to scroll should it's frame size ever change. I know one can easily do this when the keyboard pops up, because there is a notification system around that as well as the TextField delegate methods. So maybe a topic for another post...

Anyway, thank you everyone who participates on StackOverflow!



Related Topics



Leave a reply



Submit