The use of the autoresizingmask of IOS8 UIView

Source: Internet
Author: User

    • The Autoresizingmask property of the UIView is used to adapt to changes in the parent view, which in turn adjusts the frame property of the parent view.
// default is YES. If set, subviews be adjusted according to their autoresizingmask if self.bounds changes@propert Y (nonatomic) uiviewautoresizing autoresizingmask;    // Simple resize. Default is Uiviewautoresizingnone

The UIView property autoresizessubviews The default setting is Yes, and if set to Yes, when the view's bounds changes, the child view adjusts the frame property according to its own Autoresizingmask property.

    • Autoresizingmask has the following properties (the default value is Uiviewautoresizingnone):
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};

    • Example
1 #import "TestViewController.h"2 @interfaceTestviewcontroller ()3@property (nonatomic,weak) UIView *Superview;4 @end5 6 @implementationTestviewcontroller7-(void) Loadview8 {9 [Super Loadview];TenUIView * Superview = [[UIView alloc] Initwithframe:cgrectmake ( -, -, -, $)]; OneSuperview.backgroundcolor =[Uicolor Redcolor]; A [Self.view Addsubview:superview]; -Self.view.backgroundColor =[Uicolor Whitecolor]; -_superview =Superview; theUIView * SubView = [[UIView alloc] Initwithframe:cgrectmake (0, -, -, +)]; -Superview.autoresizessubviews =YES; -      - //1, set the subview with the superview bounds changes, the upper, lower, left, right margin and width of the change with the Superview changes +Subview.autoresizingmask = Uiviewautoresizingflexibletopmargin | Uiviewautoresizingflexibleheight | uiviewautoresizingflexiblebottommargin| uiviewautoresizingflexibleleftmargin| uiviewautoresizingflexiblerightmargin|Uiviewautoresizingflexiblewidth; - //2, set Subview with Superview bounds changes, the upper, lower, and high are changed with the Superview change, and the left and right margins and width unchanged +Subview.autoresizingmask = Uiviewautoresizingflexibletopmargin | Uiviewautoresizingflexibleheight |Uiviewautoresizingflexiblebottommargin; A //3, set the Subview with the superview bounds changes, the top and high are changed with the Superview change, while the bottom, left, right margin and width unchanged atSubview.autoresizingmask = Uiviewautoresizingflexibletopmargin |Uiviewautoresizingflexibleheight; - //4, a single attribute generally do not do this kind of setting (do not see what effect) - //subview.autoresizingmask = uiviewautoresizingflexibletopmargin; - //5, set the subview with the superview bounds changes, the bottom margin and the height change with the Superview change, and the upper, the left, the right margin and the width unchanged -Subview.autoresizingmask = uiviewautoresizingflexibleheight|Uiviewautoresizingflexiblebottommargin; - //6, set the subview with the superview bounds changes, the left and the width of the change with the Superview changes, and the upper, lower, right and height unchanged inSubview.autoresizingmask = uiviewautoresizingflexiblewidth|Uiviewautoresizingflexibleleftmargin; - //7, set the Subview with the superview bounds changes, the right margin and width are changed with the Superview change, and the upper, lower, left and height unchanged toSubview.autoresizingmask = uiviewautoresizingflexiblewidth|Uiviewautoresizingflexiblerightmargin; + //8, set Subview with the Superview bounds changes, left, right and width are changed with the Superview change, and the upper and lower margins and height unchanged -Subview.autoresizingmask = uiviewautoresizingflexiblewidth| uiviewautoresizingflexiblerightmargin|Uiviewautoresizingflexibleleftmargin; the  *      $Subview.backgroundcolor =[Uicolor Blackcolor];Panax Notoginseng [Superview Addsubview:subview]; -      theUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, -, -, -)]; + [btn addtarget:self Action: @selector (click:) forcontrolevents:uicontroleventtouchupinside]; A[BTN Settitle:@"Point Me"Forstate:uicontrolstatenormal]; the [btn Setbackgroundcolor:[uicolor graycolor]; +      - [Self.view addsubview:btn]; $ } $  --(void) Click: (ID) Sender - { the_superview.frame = CGRectMake ( -, -, $, $); - }Wuyi @end

    • Choose 2 parameters here to illustrate the following.
    • The 19th line of code setting Subview changes with the Superview bounds, the top, bottom, left, right margin and width height all change with the Superview change

Superview Start frame for (20, 20, 100, 200) Click button set to (50, 50, 200, 200)

Subview's frame is (0, 20, 50, 40)

Subview start distance superview top margin is 20

Subview start distance Superview left margin is 0

Subview start distance Superview bottom margin is 140

Subview start distance Superview right margin is 50

    

After the Superview frame is adjusted (50, 50, 200, 200), the subview follows the change.

Superview compared to the previous frame, the height does not change, then the Subview relative to the Superview in the upper and lower margins and the height of the natural there is no corresponding change.

And the width is changed from 100 to 200, and the corresponding Subview is increased by one more than the Superview in the left and right margins and width.

Subview distance Superview top margin is still 20*1

Subview distance Superview The left margin is 0*2

Subview distance Superview bottom margin is 140*1

Subview distance Superview right margin is 50*2

The height of the subview is 40*1

The width of the subview is 50*2

    • The 29th line of code setting Subview changes with the Superview bounds, the left margin and the width are changed with the Superview, while the top, bottom, right margin and height are unchanged

Superview Start frame for (20, 20, 100, 200) Click button set to (50, 50, 200, 200)

Subview's frame is (0, 20, 50, 40)

Subview start distance superview top margin is 20

Subview start distance Superview left margin is 0

Subview start distance Superview bottom margin is 140

Subview start distance Superview right margin is 50

    

After the Superview frame is adjusted (50, 50, 200, 200), the subview follows the change.

Superview compared to the previous frame, the height does not change, then the Subview relative to the Superview in the upper and lower margins and the height of the natural there is no corresponding change.

The width is changed from 100 to 200, and the right margin of the Subview and Superview remains unchanged. The left margin and width need to be recalculated.

Then the actual change from 100 to 200 is to ignore the original right margin (in fact, it can be understood that the property does not follow the change in the calculation process directly minus the corresponding value in the calculation of the change in multiples) then it should be from (100-50)-(200-50) =1/ 3 then the right margin and width should be 3 times times the number of previous changes.

* (100-50) The original width minus the fixed distance from the right margin; (200-50) The width minus the fixed distance from the right margin after the change.

    Subview distance Superview top margin is still 20*1

Subview distance Superview The left margin is 0*3

    Subview distance Superview bottom margin is 140*1

    Subview distance Superview right margin is 50*1

    The height of the subview is 40*1

The width of the subview is 50*3

The above for my understanding of the Autoresizingmask attribute, if there are errors or deficiencies in the place to welcome the point. (also can add oneself QQ627306590 exchange, hope many advice. )

    

The use of the autoresizingmask of IOS8 UIView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.