IOS AutoLayout Automatic Layout intermediate development tutorial (5)-Modify the constraint value and delay Loading

Source: Internet
Author: User

IOS AutoLayout Automatic Layout intermediate development tutorial (5)-Modify the constraint value and delay Loading

How do I modify the value of the autolayout constraint?

 

Currently, I know five methods. modify the frame (sometimes it may not work, but it can be animated) 2. modify the float value of the constraint. 3. use the VisualFormat Language 4. use constraintWithItem to change by magnification, for example, 2x + 1 = Y 5. remove constraints (remove at runtime) and add new constraints

The previous article has discussed how to use storyboard to create constraints. However, in actual development, we often need to adapt to different screen sizes and system versions, in this case, we need to adjust our layout with code,

First, we will introduce 2nd methods:

Directly modify the value of the constraint. This is the most direct and simplest method. It is officially recommended! It is easier to add constraints than to remove constraints!

First, drag a view to viewController, set the value of the top left width and height, and determine the position of the view:

The effects and constraints are as follows:

We can see that the constraint is: the distance is 10 on the left, 61 on the top, and the width and height are not 117,111.

After we bind the ViewController class in the figure, drag several constraints to the extension of the bound ViewController class:

How to drag? :

We tried to drag the left constraint and height constraint to the extension of viewcontroller. m.

Drag effect:

A line is generated during the drag-and-drop process. You need to enter a noun for the outlet and click connect or press Enter. This will be available in the code!

 

Original

The result of successfully dragging is:

Next we will modify the top boundary of the view and the height of the view to increase it by 100:

The Code is as follows:

 

 

//// ViewController. m // SizeClass // Created by http://blog.csdn.net/yangbingbinga 15/1/21. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga All rights reserved. // # import ViewController. h @ interface ViewController () @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * top; @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * height; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // self. top. constant ++ = 100; // self. height. constant ++ = 100;} @ end
When we comment out this code, the running effect is like this:

 


 

We can see that the width and height of this view are the same:

When we open the comment:

 

//// ViewController. m // SizeClass // Created by http://blog.csdn.net/yangbingbinga 15/1/21. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga All rights reserved. // # import ViewController. h @ interface ViewController () @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * top; @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * height; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. top. constant + = 100; self. height. constant ++ = 100;} @ end


 

The running effect is as follows:

We can see that the value of top is increased by 100 compared with the previous one, and the height is also increased by 100!

It can be noted that every

NSLayoutConstraint objects all have constant values. You can drag them into the code to directly change their values without any warning or conflict!
However, this is not complete yet, and you may encounter it during development. You have modified the constant value in viewDidLoad or modified the value of other constraints, but it has not produced any effect:
This is because the constraint you set on the storyboard is handled as follows: the code block of the constraint you modified in viewDidLoad is run, but it is overwritten by the storyboard's own configuration after running, so what you see is the constraint you set before!
Solution: delay the execution of statements that modify the constant value or constraint! Even in 0.1 seconds, you can modify the corresponding constraints after the storyboard is initially completed, so that it will not be overwritten!
For details, see the code.

 

 

//// ViewController. m // SizeClass // Created by http://blog.csdn.net/yangbingbinga 15/1/21. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga All rights reserved. # import ViewController. h @ interface ViewController () @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * top; @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * height; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initialize mselector: @ selector (modifyConstant) withObject: nil afterDelay: 0.1]; // delayed loading and execution
ModifyConstant: Change the constraint value after 0.1 seconds!
}-(Void) modifyConstant // put the modified Code in a room! {Self. top. constant + = 100; self. height. constant + = 100;} @ end

 

 
This will solve the problem of modifying the constraint value in viewDidLoad! 

 


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.