Set UIView's autoresizing mask programmatically?

前端 未结 5 1250
青春惊慌失措
青春惊慌失措 2021-02-01 00:56

I have to set autoresizingMask programmatically for UIView.

I don\'t know how to implement this.

5条回答
  •  眼角桃花
    2021-02-01 01:07

    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.

提交回复
热议问题