IOS-Autolayout automatic layout, ios-autolayout

Source: Internet
Author: User

IOS-Autolayout automatic layout, ios-autolayout
1. preface • in iOS, most view Controllers contain a large amount of code to set the UI layout and set the horizontal or vertical positions of controls, to ensure that components can be reasonably laid out in different versions of iOS • even some programmers want to use the same view Controller on different devices, which adds more complexity to the code! • The introduction of AutoLayout solves this problem well! 2. what is AutoLayout • AutoLayout is a constraint-based and descriptive layout system-based on constraints: it is different from the position and size of a previously defined frame, the location determination of AutoLayout is defined by the relative location constraints. For example, the x coordinate is the center of the superView, And the y coordinate is the top 10 pixels at the bottom of the screen.-description: the definition of constraints and the relationship between views use methods similar to natural or visual languages to describe-Layout System: used to take charge of the location of each element on the Interface • AutoLayout provides developers with a layout method different from that specified by the traditional UI element location. In the past, both drag and drop in IB and write in code, each UIView has its own frame attribute to define its position and size in the current view. The use of AutoLayout is changed to the use of constraints to define the view position and size 3. advantages of AutoLayout • solves view adaptation problems with different resolutions and screen sizes, and simplifies the definition of the position of the view during rotation. The original 10-pixel center view on the bottom, no matter when you rotate the screen or change the device (iPad, iPad mini, iPhone 4 or iPhone 5/iPhone6/iPhone6plus, it is always at the center of 10 pixels above the bottom and will not change • Use constraints to describe the layout. The view frame is calculated based on these constraints. 4. Differences between AutoLayout and Autoresizing Mask • before iOS6, the adaptation to screen rotation and the automatic adaptation of iPhone and iPad screens were basically completed by Autoresizing Mask. However, Autoresizing Mask is somewhat outdated and dull as iOS apps become increasingly demanding and various screen and resolution devices may emerge in the future. AutoLayout can complete all the work that was previously completed by Autoresizing Mask and be competent for some tasks that were not previously completed, including: • AutoLayout can specify the relative positions of any two views, instead of having two views in the immediate view hierarchy as Autoresizing Mask does not have to specify the constraint of the Equality relationship. It can specify non-equal constraints (greater than or less ); the layout that Autoresizing Mask can do can only be equal. • AutoLayout can specify the priority of the constraint. During frame calculation, the priority is calculated based on the conditions that meet the priority. Basic use of AutoLayout • After creating a constraint, you need to add it to the view to be applied.When adding a view, note that the target view must follow the following rules:: • 1) Add constraints between two views at the same level to their parent view. • 2) Constraint relationships between two views at different levels, add to their nearest parent view • 3) add constraints between two views with hierarchies to parent view with higher hierarchies. 6. Add and refresh Constraints (CODE)

 

-(Void) addConstraint :( NSLayoutConstraint *) constraint

 

• Refresh constraints changes

 

-SetNeedsUpdateConstraints

 

-LayoutIfNeeded

 


[Button setTranslatesAutoresizingMaskIntoConstraints: NO];

 

 

// 2.1 horizontal 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 constraint NSLayoutConstraint * constraintY = [NSLayoutConstraint constraintWithItem: button attribute: inclurelatedby: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeCenterY multiplier: 1.0f constant: 0.0f]; [self. view addConstraint: constraintY];

 

 

6. Errors that are prone to when AutoLayout is used • The Layout of Ambiguous Layout cannot be determined, that is, the given constraint condition cannot uniquely determine a Layout, that is, the constraint condition is insufficient, and the unique Layout result cannot be obtained. In this case, adding necessary Constraints or adjusting the priority can solve the problem. • Unsatisfiable Constraints cannot meet the Constraints. The source of the problem is that the Constraints conflict with each other and cannot be met at the same time, some constraints need to be deleted. • using IB can easily complete complex constraints, and there will be very few omissions or extra constraints in actual development, the problematic constraints will receive errors or warnings directly in IB. Author: Clear Saup
Source: http://www.cnblogs.com/qingche/
The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide a connection to the original article on the article page.

 

 

 

 

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.