UIView autoresizingMask - Interface Builder for Code - Programmatically create struts and springs - Swift or Objective-C

I developed some subheadings using Interface Builder, but I would like to do this instead of code.

I read the UIView docs about setting the view.autoresizingMask property. I am looking for a logical explanation of how to translate struts and springs using various masks (e.g. UIViewAutoresizingFlexibleLeftMargin , etc.).

+50
ios objective-c swift uiview autoresizingmask
May 6 '12 at 5:33
source share
2 answers

When setting up an autoresist mask for a view, use the bitwise inclusion OR ( | ) (Objective-C) or an array (Swift 2, 3) to specify springs and spacers .

  • Springs are represented by an indication of the mask (Objective-C or Swift 3, respectively):

    • vertical spring: UIViewAutoresizingFlexibleHeight or .flexibleHeight

    • horizontal spring: UIViewAutoresizingFlexibleWidth or .flexibleWidth

  • Struts are represented by the absence of one of four “flexible fields” (that is, if the rack does not exist, the mask for this field is specified):

    • UIViewAutoresizingFlexibleLeftMargin or .flexibleLeftMargin

    • UIViewAutoresizingFlexibleRightMargin or .flexibleRightMargin

    • UIViewAutoresizingFlexibleTopMargin or .flexibleTopMargin

    • UIViewAutoresizingFlexibleBottomMargin or .flexibleBottomMargin

For example, a view with a horizontal row spring and top and bottom rows will have a width, and the left and right margins indicated as flexible:

Swift 3

 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); 

enter image description here

+118
May 6 '12 at 12:26
source share

UIViewAutoResizingMasks is what we call "struts" and "springs". Consider this: you have a large square with a small square inside. To keep this square perfectly centered, you must set a fixed width from each inner edge of the large square to limit it. These are racks.

Springs, on the other hand, is more like a UIView during rotation. Say our gaze should remain at the bottom of the screen, centered (like the UIToolbar). We want to keep it top spring flexible so that when you rotate the view from 460 px to 320 px, it will keep it in the same position relative to the screen, which is now changing. Selecting one of these springs in IB is equal to setting the corresponding UIViewAutoResizingMask and highlighting the top spring, in particular, akin to calling myView.autoResizingMask = UIViewAutoresizingFlexibleTopMargin .

Values ​​can be used in tandem by enclosing them in a pair of brackets and using the operator or, for example, myView.autoResizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)

Masks tell you numbers because they are typdef NSUInteger, and the flags that apple assigned to them. Cmd + click on one to see its defintion method.

+8
May 6 '12 at 5:54
source share



All Articles