IOS AutoLayout Automatic Layout intermediate development tutorial (5)-Modify the constraint value, delayed loading, iosautolayout
How do I modify the value of the autolayout constraint?
<Span style = "font-family: SimSun;"> currently, I have five known 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 </span>
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) viewDi DLoad {[super viewDidLoad]; [self generated mselector: @ selector (modifyConstant) withObject: nil afterDelay: 0.1]; // delayed loading, run the <pre name = "code" class = "cpp" style = "font-size: 14px; line-height: 26px;"> modifyConstant command to 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!
If you have more questions, please join us! Original address http://blog.csdn.net/yangbingbinga