Autoresizingmask is a property of UIView, and in some simple layouts, using autoresizingmask, you can implement automatic layout of child controls relative to the parent control.
The autoresizingmask is of type uiviewautoresizing, which is defined as:
@property (nonatomic) uiviewautoresizing autoresizingmask; // Simple resize. Default is Uiviewautoresizingnone
Uiviewautoresizing is an enumeration type, the default is Uiviewautoresizingnone, which can be obtained with the following values:
typedef ns_options (Nsuinteger, uiviewautoresizing) {Uiviewautoresizingnone=0, Uiviewautoresizingflexibleleftmargin=1<<0, Uiviewautoresizingflexiblewidth=1<<1, Uiviewautoresizingflexiblerightmargin=1<<2, Uiviewautoresizingflexibletopmargin=1<<3, Uiviewautoresizingflexibleheight=1<<4, Uiviewautoresizingflexiblebottommargin=1<<5};
Each attribute explains:
Uiviewautoresizingnone |
Does not change with the parent view |
Uiviewautoresizingflexibleleftmargin |
Automatically adjusts the left margin of the view and parent views to ensure that the right margin is constant |
Uiviewautoresizingflexiblewidth |
Automatically adjusts the width of the view to ensure that the left and right margins are constant |
Uiviewautoresizingflexiblerightmargin |
Automatically adjusts the view to the right margin of the parent view to ensure that the left margin is constant |
Uiviewautoresizingflexibletopmargin |
Automatically adjusts the top margin of the view and parent views to ensure that the bottom margin is not changed |
Uiviewautoresizingflexibleheight |
Automatically adjusts the height of the view to ensure that the top and bottom margins are constant |
Uiviewautoresizingflexiblebottommargin |
Automatically adjusts the bottom margin of view and parent to ensure that the top margin is not changed |
Note: Autoresizingmask can be used either directly in the code or in Uistoryboard.
An example of using Autoresizingmask in a code:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; Uiviewcontroller*viewcontroller =[[Uiviewcontroller alloc] init]; Self.window.rootViewController=Viewcontroller; Self.window.backgroundColor=[Uicolor Whitecolor]; UIView*view = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $, -)]; [View Setbackgroundcolor:[uicolor Graycolor]; [Self.window Addsubview:view]; UIButton*button = [[UIButton alloc] Initwithframe:cgrectmake ( -, -, -, -)]; [Button Setbackgroundcolor:[uicolor Whitecolor]; [View Addsubview:button]; //The right margin from the parent view is unchanged//button.autoresizingmask = Uiviewautoresizingflexibleleftmargin; //left margin unchanged from parent view//button.autoresizingmask = Uiviewautoresizingflexiblerightmargin; //the left and right margins of the parent view are constant, and the button size adjusts//button.autoresizingmask = uiviewautoresizingflexiblewidth; //view.frame = CGRectMake (20,100,300,100); //the bottom margin is unchanged from the parent view//button.autoresizingmask = Uiviewautoresizingflexibletopmargin; //the top margin is not changed from the parent view//button.autoresizingmask = Uiviewautoresizingflexiblebottommargin; //the top and bottom margins of the parent view are unchanged, and the button size adjustsButton.autoresizingmask =Uiviewautoresizingflexibleheight; View.frame= CGRectMake ( -, -, $, $); [Self.window makekeyandvisible]; returnYES;}
In addition, Autoresizingmask can be used in combination. For example:
Button.autoresizingmask = Uiviewautoresizingflexiblebottommargin | Uiviewautoresizingflexibleleftmargin;
Indicates that the child controls are unchanged relative to the top and right of the parent control.
IOS--Autoresizingmask use (GO)