Set UIView's autoresizing mask programmaticall

2019-03-09 06:40发布

I have to set autoresizingMask programmatically for UIView.

I don't know how to implement this.

enter image description here

5条回答
啃猪蹄的小仙女
2楼-- · 2019-03-09 06:56

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.

Cheers.

查看更多
虎瘦雄心在
3楼-- · 2019-03-09 06:59

Swift 2.0:

view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin]
查看更多
混吃等死
4楼-- · 2019-03-09 06:59

Swift 4.1:

view.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]
查看更多
Rolldiameter
5楼-- · 2019-03-09 07:04

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

查看更多
该账号已被封号
6楼-- · 2019-03-09 07:10

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

autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
查看更多
登录 后发表回答