1. Preface• In iOS programs, most view controllers contain a lot of code to set the UI layout, set the horizontal or vertical position of the controls, to ensure that the components are properly laid out in different versions of iOS • Even some programmers want to use the same view controller on different devices This adds more complexity to the code! • The introduction of automatic layout AutoLayout a good solution to this problem!2. What is AutoLayout•AutoLayout is a constraint-based, descriptive layout system– based on constraints: unlike previously defined frame positions and dimensions, the position of the AutoLayout is defined by the so-called relative position constraints, such as the center of the x-coordinate superview, and the y-coordinate of 10 pixels above the bottom of the screen. Descriptive: the definition of constraints and the relationship of each view are described using approaches that are close to natural language or visual language – layout system: The position of each element that is responsible for the interface AutoLayout provides developers with a different layout method than traditional for UI element placement. Previously, whether you were dragging or dropping in IB or writing in code, each uiview had its own frame property to define its position and size in the current view. Using AutoLayout, it becomes the use of constraints to define the location and dimensions of the viewAdvantages of 3.AutoLayout• Solves the problem of view adaptation in different resolutions and screen sizes, while simplifying the definition of the position of the view when rotating. The view, centered 10 pixels above the bottom, is always centered 10 pixels above the bottom, whether it's rotating the screen or changing the device (ipad, ipad Mini, IPhone 4, or Iphone5/iphone6/iphone6plus). Will not change• Use constraints to describe the layout, and the frame of the view will be calculated based on these constraints4. the difference between AutoLayout and autoresizing mask• Before iOS6, the adaptation to screen rotation and the automatic adaptation of the Iphone,ipad screen are basically done by autoresizing mask. But with the growing demand for iOS apps, and the variety of screen and resolution devices that may appear in the future, autoresizing mask is a bit outdated and dull. The AutoLayout can do all the work that the original autoresizing mask can do, and also be able to perform tasks that were previously impossible, including the following: AutoLayout can specify the relative position of any two view without needing to be like a autoresizing Mask that requires two view in the immediate view hierarchy AutoLayout does not have to specify an equality relationship constraint, it can specify a non-equality constraint (greater than or less than, etc.), and autoresizing mask can only be made of the layout is equal conditions · The AutoLayout can specify the precedence of the constraint, and the calculation of the frame takes precedence over the conditions that meet the high priority5. Basic use of AutoLayout • After you create a constraint, you need to add it to the view you want to work on.
be aware that the following rules apply to the target view when adding: • 1) for the constraint relationship between the two same-level view, add to their parent view • 2) for the constraints of two different levels of view, add to their nearest common parent view • 3) for a hierarchical relationship between two view constraints, Add to a higher-level parent view6. Adding and refreshing Constraints (code)
-(void) AddConstraint: (Nslayoutconstraint *) constraint
• Changes to the refresh constraint
-setneedsupdateconstraints
-layoutifneeded
[Button Settranslatesautoresizingmaskintoconstraints:no];
2.1 Horizontal Direction constraint nslayoutconstraint *constraintx = [Nslayoutconstraint Constraintwithitem:button attribute: Nslayoutattributecenterx relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecenterx multiplier:1.0f constant:0.0f]; [Self.view addconstraint:constraintx];//2.2 vertical-oriented constraint nslayoutconstraint *constrainty = [Nslayoutconstraint Constraintwithitem:button attribute:nslayoutattributecentery relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecentery multiplier:1.0f constant:0.0f]; [Self.view Addconstraint:constrainty];
6. using AutoLayout prone errors ambiguous layout can not be determined, that is, the constraints given can not uniquely determine a layout, that is, constraints are insufficient to obtain a unique layout results. This situation generally add some necessary constraints or adjust the priority to solve the unsatisfiable Constraints can not meet the constraints, the source of the problem is that there are constraints conflicting with each other, so can not be satisfied at the same time, need to delete some constraints • Now using IB can be easier to complete complex constraints , in the actual development rarely meet the omission or redundant constraints, the problem of constraints will be directly in the IB to get errors or warnings. Clear Saup
Source: http://www.cnblogs.com/qingche/
This article is copyright to the author and the blog Park is shared, welcome reprint, but must retain this paragraph statement, and in the article page obvious location to give the original text connection.
Ios-autolayout Automatic Layout