04-masonry Use Practice

Source: Internet
Author: User

As the size of the iphone's screen becomes more diverse, the screen-fitting work in the development process becomes increasingly important.

But the system comes with the AutoLayout, the code is cumbersome.

Masonry is a lightweight layout framework that re-describes automatic layout with more concise syntax and is more readable.

The attributes supported by masonry are:

Exercise 1:

1- (void) Viewdidload {2 [Super Viewdidload];3     //additional setup after loading the view, typically from a nib.4     5UIView *view1 =[[UIView alloc] init];6View1.backgroundcolor =[Uicolor Redcolor];7 [Self.view Addsubview:view1];8     //adding constraints to View19[View1 mas_makeconstraints:^ (Masconstraintmaker *Make ) {Ten Make.center.equalTo (self.view); OneMake.size.mas_equalTo (Cgsizemake ( -, -)); A    }]; -}

The results are as follows:

Exercise 2:

1- (void) Viewdidload {2 [Super Viewdidload];3     //additional setup after loading the view, typically from a nib.4     5UIView *view1 =[[UIView alloc] init];6View1.backgroundcolor =[Uicolor Redcolor];7 [Self.view Addsubview:view1];8     //adding constraints to View19[View1 mas_makeconstraints:^ (Masconstraintmaker *Make ) {Ten Make.center.equalTo (self.view); OneMake.size.mas_equalTo (Cgsizemake ( -, -)); A    }]; -     -UIView *view2 =[[UIView alloc] init]; theView2.backgroundcolor =[Uicolor Yellowcolor]; - [View1 addsubview:view2]; -     //to add a constraint to View2 -[View2 mas_makeconstraints:^ (Masconstraintmaker *Make ) { + //Make.center.equalTo (view1); -Make.edges.equalTo (View1). With.insets (Uiedgeinsetsmake (Ten, -, +, -)); +          A     }]; at      -      -}

The results are as follows: (set the top-left and bottom-right padding for yellow view to 10, 20, 40, 60, respectively)

Exercise 3:

1- (void) Viewdidload {2 [Super Viewdidload];3   4     intpadding =Ten;5     6UIView *view1 =[[UIView alloc] init];7View1.backgroundcolor =[Uicolor Redcolor];8 [Self.view Addsubview:view1];9     TenUIView *view2 =[[UIView alloc] init]; OneView2.backgroundcolor =[Uicolor Yellowcolor]; A [Self.view addsubview:view2]; -      -      the     //adding constraints to View1 -[View1 mas_makeconstraints:^ (Masconstraintmaker *Make ) { - Make.centerY.mas_equalTo (Self.view.mas_centerY); - Make.left.equalTo (self.view.mas_left). With.offset (padding); +Make.right.equalTo (View1.mas_left). With.offset (-padding); -Make.height.mas_equalTo (@ -); + Make.width.equalTo (VIEW2); A          at     }]; -      -      -     //to add a constraint to View2 -[View2 mas_makeconstraints:^ (Masconstraintmaker *Make ) { - Make.centerY.equalTo (Self.view.mas_centerY); in Make.left.equalTo (view1.mas_right). With.offset (padding); -Make.right.equalTo (self.view.mas_right). With.offset (-padding); toMake.height.equalTo (@ -); + Make.width.equalTo (view1); -          the          *     }]; $     Panax Notoginseng}

The results are as follows: Automatic calculation of the width of red and yellow view by adding constraints

Exercise 4:

1- (void) Viewdidload {2 [Super Viewdidload];3   4     //order some view in ScrollView and calculate Contentsize5Uiscrollview *scrollview =[[Uiscrollview alloc] init];6Scrollview.backgroundcolor =[Uicolor Yellowcolor];7 [Self.view Addsubview:scrollview];8     9     //adding constraints to ScrollViewTen[ScrollView makeconstraints:^ (Masconstraintmaker *Make ) { OneMake.edges.equalTo (Self.view). With.insets (Uiedgeinsetsmake (Ten,Ten,Ten,Ten)); A          -     }]; -      theUIView *container =[[UIView alloc] init]; - [ScrollView Addsubview:container]; -      -     //adding constraints to container +[Container makeconstraints:^ (Masconstraintmaker *Make ) { - Make.edges.equalTo (scrollView); + Make.width.equalTo (scrollView); A     }]; at      -     intCount =Ten; -UIView *lastview =Nil; -      for(inti =1; I <= count; i++) { -UIView *subview =[[UIView alloc] init]; - [Container Addsubview:subview]; in          -Subview.backgroundcolor =Shrandomcolor; to          +[SubView makeconstraints:^ (Masconstraintmaker *Make ) { - Make.left.and.right.equalTo (container); theMake.height.equalTo (@ ( -*i)); *             if(Lastview) { $ Make.top.equalTo (lastview.bottom);Panax Notoginseng}Else{ - Make.top.equalTo (container.top); the             } +                  A         }]; theLastview =SubView; +     } -      $      $[Container makeconstraints:^ (Masconstraintmaker *Make ) { - Make.bottom.equalTo (lastview.bottom); -     }]; the}

Reference:

Http://www.cocoachina.com/ios/20141219/10702.html

04-masonry Use Practice

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.