59

I have to set autoresizingMask programmatically for UIView.

I don't know how to implement this.

enter image description here

DrummerB
  • 39,814
  • 12
  • 105
  • 142
Hiren
  • 12,720
  • 7
  • 52
  • 72

5 Answers5

103

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.

Nikita
  • 460
  • 5
  • 11
pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • No one has mentioned WHERE to set this value. What method(s) are suggested for setting this in? – Logicsaurus Rex Dec 29 '15 at 05:32
  • 1
    @LogicsaurusRex you usually set the autoresizing mask when you are creating the view. It can be set anywhere else though, as long as the view is not nil. It can also be set in InterfaceBuilder – pnizzle Dec 29 '15 at 07:35
  • @LogicsaurusRex as for the methods in objectiveC, you use the dot syntax as shown in my answer or you can use setAutoResizingMask – pnizzle Dec 29 '15 at 07:37
  • @Bharath your edit is wrong. The screen-shot specifies a non flexible width and non flexible height. Not sure how that was approved. Please see my fix. – pnizzle Jun 06 '16 at 01:42
24

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 |.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • i have to set image in top with AspectFit mode. I get the image properly but the AspecFit property is not working like image is in center but the left and right part is cut or out of bound – Hiren Oct 02 '12 at 17:49
  • 4
    While this answer helps those who want to know the possible values of UIAutoResizingMask, the solution provided does not match what is in the screen shot. The top margin should be fixed and every other side left flexible (not width or height, just the sides). See my answer for solution that matches the screenshot – pnizzle Nov 12 '13 at 06:35
22

Swift 2.0:

view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin]
Thomás Pereira
  • 9,589
  • 2
  • 31
  • 34
6

Swift 4.1:

view.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]
Jimmy_m
  • 1,568
  • 20
  • 24
0

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

autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;