Set UIView's autoresizing mask programmatically?

IphoneObjective CIosUiviewAutoresizingmask

Iphone Problem Overview


I have to set autoresizingMask programmatically for UIView.

I don't know how to implement this.

enter image description here

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

You have to set the view's autoresizingMask property:

view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

The possible values are defined in UIViewAutoresizing:

enum {
   UIViewAutoresizingNone                 = 0,
   UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
   UIViewAutoresizingFlexibleWidth        = 1 << 1,
   UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
   UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
   UIViewAutoresizingFlexibleHeight       = 1 << 4,
   UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

You can set multiple values with the bitwise OR operator |.

Solution 3 - Iphone

Swift 2.0:

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

Solution 4 - Iphone

Swift 4.1:

view.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]

Solution 5 - Iphone

To set flexible Top Margin,Bottom Margin,Left Margin and Right Margin of a UIView write the following code-

autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHirenView Question on Stackoverflow
Solution 1 - IphonepnizzleView Answer on Stackoverflow
Solution 2 - IphoneDrummerBView Answer on Stackoverflow
Solution 3 - IphoneThomás PereiraView Answer on Stackoverflow
Solution 4 - IphoneJimmy_mView Answer on Stackoverflow
Solution 5 - Iphoneuser4919266View Answer on Stackoverflow