iOS Development-ui Chapter-autolayout

Source: Internet
Author: User

1. Overview : as iOS devices update iterations , screen- fitting issues become increasingly important , AutoLayout. only after Xcode4.1 and OS X10. 7.

2.Brief introduction: AppleProvides a2Type of layoutAutoLayout, xib; Another way to layout ,apple2:nslayoutmanagernslayoutconstraintnslayoutmanagernstextstorage,nstextcontainer implement nscoding protocol used to layout text view span class= "s2" >,nslayoutconstraint

3. principle : The main idea of AutoLayout is restraint. In the Nslayoutconstraint class , there is one of the most important class methods

+ (Instancetype) Constraintwithitem: (ID) view1 attribute: (nslayoutattribute) attr1 Relatedby: ( Nslayoutrelation) Relation Toitem: (ID) view2 attribute: (nslayoutattribute) attr2 multiplier: (cgfloat) Multiplier constant: (cgfloat) C;

The formula that is implemented is "VIEW1.ATTR1 = view2.attr2 * multiplier + constant". Establishes a constraint relationship between 2 views (the second view can be nil). But using this method is simple and clear, the disadvantage is too cumbersome and complex . However, we recommend using the VFL for layout.

4.1 content :

1). When we use autolayout , We want to abandon auto fit settings , =no; Otherwise it will cause the view to clutter.

2). Compliance with high cohesion uiview.h-(void< Span class= "S3") updateconstraints; uiviewcontroller.h-( void) Updateviewconstraints;overrides must call super or send-updateconstraints to the View=> i.e. [ Super Updateviewconstraints];

3). The most basic way to add constraints : Create a View object , create a nslayoutconstraint constraint object , Constraintwithitem Constraints are set and constraints are added to the view object.

4.2 content :

1). AutoLayout also provides the VFL (Visual Format Language) to constrain , + (Nsarray *) Constraintswithvisualformat: ( NSString *) format options: (nslayoutformatoptions) OPTs metrics: (nsdictionary *) metrics Views: (Nsdictionary *) views;

//format: pass in a separate constraint statement for each view; query xcode api visual Format syntax

2). Autolayout step : Create a view, And added to the parent view to show->[parent View addconstraints: ( constraint array Span class= "S3" >) ]-> incoming parameters, format concrete constraint statement ,opts can be set to nil,metrics incoming distance parameter ( with parameters instead of magic Pass in the corresponding view control ,metricsviews service.

3). Example 1:

- (void) updateviewconstraints{[Super updateviewconstraints];//Override InitializationUIView*view1 =[[UIView alloc] init]; View1.translatesautoresizingmaskintoconstraints= NO;//Cancel Automatic LayoutView1.backgroundcolor =[Uicolor Bluecolor];        [Self.view Addsubview:view1]; Nsdictionary*views_dic = @{@"View1": View1};//Control of ConstraintsNSString*hlayoutstr =@"h:|-(==100)-[view1 (==100)]";//H: Represents landscape, | represents relative to the parent view,-represents the constraint distance, () All of the constraint distances, [] inside the View object that represents the constraint, this sentence means: horizontal from left to right, relative to the parent view distance of 100 pixels, the width of view1NSString *vlayoutstr =@"v:|-(==100)-[view1 (==50)]";//V: Represents portrait, | represents relative to the parent view,-represents the constraint distance, () All of the constraint distances, [] inside the View object that represents the constraint, this sentence means: vertical from top to bottom, relative to the parent view distance of 100 pixels, the height of the View1Nsarray*hlayoutarr = [Nslayoutconstraint constraintswithvisualformat:hlayoutstr options:0Metrics:nil Views:views_dic]; Nsarray*vlayoutarr = [Nslayoutconstraint constraintswithvisualformat:vlayoutstr options:0Metrics:nil Views:views_dic];    [Self.view Addconstraints:hlayoutarr]; [Self.view Addconstraints:vlayoutarr];}

Example 2:

- (void) updateviewconstraints{[Super updateviewconstraints];//Override InitializationUIView*view1 =[[UIView alloc] init]; View1.translatesautoresizingmaskintoconstraints=NO; View1.backgroundcolor=[Uicolor Bluecolor];        [Self.view Addsubview:view1]; UIView*view2 =[[UIView alloc] init]; View2.translatesautoresizingmaskintoconstraints=NO; View2.backgroundcolor=[Uicolor Greencolor];        [Self.view Addsubview:view2]; Nsdictionary*views_dic = @{@"View1": View1,@"View2": View2}; Nsdictionary*views_metrics = @{@"Top":@ -,@" Right":@ -}; NSString*hlayoutstr =@"h:|-(==100)-[view1 (==100)]-[view2 (view1)]-right-|";//means: Starting from the left side of the parent view, constrain 100 pixels, constrain the width of the view1 to 100 pixels, constrain the width of the view2 and View1, and constrain the distance from the right side of the parent view to 20 pixels. NSString *vlayoutstr =@"v:|-(-[view1) (==100)]-[view2 (==100)]"; Nsarray*hlayoutarr = [Nslayoutconstraint constraintswithvisualformat:hlayoutstr options:0Metrics:views_metrics Views:views_dic]; Nsarray*vlayoutarr = [Nslayoutconstraint constraintswithvisualformat:vlayoutstr options:0Metrics:views_metrics Views:views_dic]; [Self.view Addconstraints:hlayoutarr];//Horizontal Constraint[Self.view Addconstraints:vlayoutarr];//Vertical Constraints}

iOS Development-ui Chapter-autolayout

Related Article

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.