Using AutoLayout practices

Source: Internet
Author: User

In the layout of the interface, we often use AutoLayout to layout the interface. In terms of AutoLayout layout implementations, there are generally three ways that we often use them:

    • With Apple's powerful visual interface (Interface Builder), the layout of the interface view is done using AutoLayout.
    • Use AutoLayout in the form of pure code, that is, Nslayoutconstraint.
    • Use the third-party interface layout Library masonry for code layout.
      The following combination of a login interface to build, using the first way to introduce AutoLayout.
      Let's look at the login screen we're going to build:

      First look at the structure of this, and then we use the AutoLayout layout constraints to implement.

1. Adding a background Uiimageview constraint


The value of the with,height,x,y of this Uiimageview control is determined by fixing the relative position of the parent control. In fact, adding a constraint to a control, regardless of how many constraints are added, is ultimately the four values that determine it.
After adding the constraint, if there is no constraint conflict, the control is surrounded by a yellow line, then update the constraint, the control can be automatically layout display, if you add a constraint after the red line, then there is a constraint conflict, then you need to resolve the conflict, you can click the small red arrow, Resolve conflicts based on conflicting prompts. Shortcut keys for updating constraints: SHIFT + COMMAND + =.

2. Constraint additions to the Login box section
Here the "login box" is used as a container to wrap some of the inner child controls. The advantage of this is that when the contents of the login box need to be moved as a whole, you don't have to worry about the constraints inside the login box. The login box UIView added constraints such as:

The four constraints of the w,h,x,y in the Sign box layout The location of the login boxes section. Once you have laid out the location of the parent control, you can lay out the position of the child controls inside. The descenndant constraint in a constraint is the location of the child control and the parent control that is described.
In this constraint display, there is a constraint relationship between the control and the control. Generally we add a constraint to a child control that is added relative to the parent control, and if we are adding a constraint between adjacent controls, we can change the constraint relationship with the following small arrows:

We can also add constraints to the popup context menu by selecting the control to which you want to add the constraint, and then holding down the control key and dragging the line onto the controls to which you want to associate the constraint. Such as:

The constraint details for the controls inside the login box are no longer detailed.
3. Adding a part of the Registration box constraint
The registration box is not shown here, when added here, there is a small trick is to first the registration box in the visual scope of the internal child control to add the constraints, and then add and the adjacent control and the parent control of the constraint relationship. If it is not displayed at the time of layout, it cannot be displayed when it is running, then it needs to change the constraint and make it visible when it is running. When you click [Register Account] here, the Registration box section is visible on the screen, and the login box disappears from the screen. The implementation is a constraint that changes the left side of the login box to the left of the parent control. The code is as follows:

LeftMargin is the constraint on the left side of the login box from the left side of the parent control//@property (weak, nonatomic) Iboutlet nslayoutconstraint *leftmargin;    if( Self. LeftMargin. Constant) { Self. LeftMargin. Constant=0; button. Selected=NO; }Else{ Self. LeftMargin. Constant=  - Self. View. Width; button. Selected=YES; }//Achieve animation effects[UIViewAnimatewithduration:0.25animations:^{[ Self. ViewLayoutifneeded]; }];

4. Bottom quick Login The overall part of the layout constraints
Let's talk about the layout of the three login buttons here. These three buttons are equal width equal to the height displayed in the screen. There are many layout constraint scenarios. The first button can be fixed to the left margin constraints, upper and lower spacing constraints, and then add three buttons, such as width constraints, the first button to add the right margin constraints, the first button constraint has been added, but because the constraints of the overall constraints are not enough, the width is not calculated, the constraints are not complete, The constraint of the second button can be added and the first button is equal to the left margin of the third button. Finally, add a constraint to the third button, equal to the second button, and the right margin equals the right margin of the parent control. Then the constraint is complete and the constraint can be updated.
The text and pictures of this button are arranged in the left and right, and are arranged up and down when displayed. Here, a custom button is used to rearrange the child controls in the button. QUICKLOGINBUTTON.M main code is as follows:

-(void) layoutsubviews{[Super Layoutsubviews];Picture Self. ImageView. CenterX= Self. Width*0.5;Self. ImageView. Y=0;Text Self. Titlelabel. x=0;Self. Titlelabel. Width= Self. Width;Self. Titlelabel. Y= Self. ImageView. Height;Self. Titlelabel. Height= Self. Height-Self. ImageView. Height;}

Here's a look at the layout of the Xib on different devices:

Using AutoLayout practices

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.