IOS development UI layout and iosui Layout

Source: Internet
Author: User

IOS development UI layout and iosui Layout

This article will be updated on a regular basis. With my in-depth study of the layout, I will constantly add new knowledge, new usage skills, and new understandings.

 

1. Autoresizing (used in code)

First, let's take a look at the following code and the running results on the left. Then, we will rotate the iPhone simulator and then look at the results. Then, we can take a look at the autoresizingMask attribute in the code to see it:

After the landscape is displayed, the default frame (100,500,) of the landscape is changed to the landscape position, which is in the middle by default. However, the autoresizingMask attribute is set to auto scaling on the left and above:

The above is the basic autoresizing in the code.

 

2. Use Autolayout (used in code)

Before Autolayout, Autoresizing can be used for screen adaptation, but it has many limitations and some tasks cannot be completed at all (Autoresizing can only set the relative relationship between itself and its parent control ).

In contrast, Autolayout is much more powerful than Autoresizing, and it can solve the relationship between any controls.

 

Two core concepts of AutoLayout:

① Constraint: You can add constraints to the control to determine the position and size of the control.

To use AutoLayout, you need to create a constraint class to create a constraint object:

NSLayoutConstraint * leftLC = [NSLayoutConstraint constrainWithItem: ......];

② Reference: Who will add the constraint (can be a parent control or a sibling control ).

 

If Autolayout is used, you do not need to consider the Frame value.

 

In actual development, if you want to use AutoLayout, pay attention to one problem. Some Apple Cocoa frameworks provide UIView or custom UIView, which may have the relevant Autoresizing by default, the AutoLayout code you added later may conflict with each other, so you need the following code to automatically convert Autoresizing to AutoLayout:

Steps for code implementation of Autolayout

Then the following code uses AutoLayout to achieve the following results. Of course, the layout should be like this when rotating to a portrait screen. Otherwise, what is the significance of automatic layout:

  

Before implementing the code, I need to add some knowledge:

To use automatic layout, you must observe the following constraints:

Rules for adding constraints (1)

After creating a constraint, you need to add it to the view
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.

    
Rules for adding constraints (2)
2) Add constraints between two views of different levels to their closest parent view.
    

Rules for adding constraints (3)
3) add constraints between two views with hierarchies to parent views with higher hierarchies.
    
In the following code, I will extract a part to understand the meaning of adding rules to the above constraints:

    

Then, the meanings of the classes with this constraint and the parameters of the created object are added:

An NSLayoutConstraint object represents a constraint.
Common methods for creating Constraint Objects

        
View1: controls to be constrained
Attr1: the type of the constraint)
Relation: relationship with the reference Control
View2: controls to be referenced
Attr2: the type of the constraint)
Multiplier: multiplier
C: Constant

The complete implementation code is as follows:

1-(void) viewDidLoad {2 [super viewDidLoad]; 3 // 1. create a Blue view 4 UIView * blueView = [[UIView alloc] init]; 5 blueView. backgroundColor = [UIColor blueColor]; 6 // constraint 7 blueView that automatic conversion from autoresizing to autolayout is prohibited. translatesAutoresizingMaskIntoConstraints = NO; 8 [self. view addSubview: blueView]; 9 10 // 2. create a red view 11 UIView * redView = [[UIView alloc] init]; 12 redView. backgroundColor = [UIColor redColor]; 13 // constraints for disabling autoresizing from automatically converted to autolayout 14 redView. translatesAutoresizingMaskIntoConstraints = NO; 15 [self. view addSubview: redView]; 16 17 // set constraints 18/****** Blue view constraints *****/19 // constraint 20 on the left // constraint 21 NSLayoutConstraint * leftlc_ B = \ 22 on the left [NSLayoutConstraint constraintWithItem: blueView 23 attribute: NSLayoutAttributeLeft 24 relatedBy: NSLayoutRelationEqual 25 toItem: self. view 26 attribute: NSLayoutAttributeLeft 27 multiplier: 1.0 28 constant: 30]; 29 [self. view addConstraint: leftlc_ B]; 30 31 // bottom constraint 32 NSLayoutConstraint * bottomlc_ B = \ 33 [NSLayoutConstraint constraintWithItem: blueView 34 attribute: Limit 35 relatedBy: Limit 36 toItem: self. view 37 attribute: NSLayoutAttributeBottom 38 multiplier: 1.0 39 constant:-30]; 40 [self. view addConstraint: bottomlc_ B]; 41 42 // constraints 43 NSLayoutConstraint * rightlc_ B = \ 44 [constraint constraintWithItem: blueView 45 attribute: Limit 46 relatedBy: Limit 47 toItem: redView 48 attribute: NSLayoutAttributeLeft 49 multiplier: 1.0 50 constant:-30]; 51 [self. view addConstraint: rightlc_ B]; 52 53 // width constraint 54 NSLayoutConstraint * wlc_ B = \ 55 [constraint constraintWithItem: blueView 56 attribute: Limit 57 relatedBy: Limit 58 toItem: redView 59 attribute: NSLayoutAttributeWidth 60 multiplier: 1.0 61 constant: 0]; 62 [self. view addConstraint: wlc_ B]; 63 64 // height constraint 65 NSLayoutConstraint * hlc_ B = \ 66 [NSLayoutConstraint constraintWithItem: blueView 67 attribute: Limit 68 relatedBy: Limit 69 toItem: nil 70 attribute: NSLayoutAttributeNotAnAttribute 71 multiplier: 0.0 72 constant: 50]; 73 [blueView addConstraint: hlc_ B]; 74 75/***** red view constraint *****/76 // The right side constraint 77 NSLayoutConstraint * rightlc_r = \ 78 [NSLayoutConstraint constraintWithItem: redView 79 attribute: NSLayoutAttributeRight 80 relatedBy: NSLayoutRelationEqual 81 toItem: self. view 82 attribute: NSLayoutAttributeRight 83 multiplier: 1.0 84 constant:-30]; 85 [self. view addConstraint: rightlc_r]; 86 87 // top alignment 88 NSLayoutConstraint * toplc_r = \ 89 [constraint constraintWithItem: redView 90 attribute: 91 relatedBy: Limit 92 toItem: blueView 93 attribute: NSLayoutAttributeTop 94 multiplier: 1.0 95 constant: 0]; 96 [self. view addConstraint: toplc_r]; 97 98 // bottom alignment 99 NSLayoutConstraint * bottomlc_r = \ 100 [descriconstraintwithitem: redView101 attribute: descrirelatedby: descritoitem: blueView104 attribute: 1.0106 constant: 0]; 107 [self. view addConstraint: bottomlc_r]; 108 109}

 

 

Supplement:

  

 

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.