How do I modify the value of a AutoLayout constraint?
<span style= "Font-family:simsun;" > There are 5 kinds of methods I have known 1. Modify the frame (sometimes it may not work, but can animate) 2. Modify the value of the float for the constraint 3. Use Visualformat language 4. Use Constraintwithitem, change by magnification such as 2x+1=y 5. Remove the constraint (remove at runtime), add a new constraint </span>
The previous article has talked about how to use storyboard to create constraints, but in the actual development we often need to adapt to different screen sizes and system versions, then we need to use code to adjust our layout appropriately,
Let's introduce the 2nd method first:
Directly modify the value of the constraint, this is the most straightforward, the most straightforward, the official recommended Method! It's a lot easier to add constraints than remove constraints!
First, let's drag a view to the Viewcontroller, set the value on the left-wide height, and determine the location of the view:
The effect and constraint values are as follows:
As you can see, the constraint is: 10 from the left, 61 on the top, and a width of 117,111
After we have Viewcontroller bound classes in the diagram, drag several constraints to the extension of the bound Viewcontroller class:
How to drag?:
We're trying to drag the left and height constraints to the extension of the VIEWCONTROLLER.M.
Drag effect:
Drag the process will produce a line, let go after you need to fill in this outlet a noun, click Connect or enter, the code is there!
Original
The final effect of the drag-and-drop success in the code is:
Let's change the top edge of the view, and the height of the view so that it increases 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 on this code, it works like this:
As you can see, the width of the view we set is the same:
When we open the note:
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 results are as follows:
You can see the top value, and the previous ratio has increased by 100, and the height has increased by 100!
It can be explained that every
Nslayoutconstraint object, all have constant value, we drag into the code can change its value directly, do not produce any warning and conflict!
However, this is not the end, you may encounter during development, modify the constant value in Viewdidload, or modify the values of other constraints without producing any effect:
This is because the constraints you set in the storyboard are handled as follows: the code block for the constraint you modified in the Viewdidload is run, but after it has been run, it is storyboard its own configuration to overwrite, so What you see is the constraint you set before!
WORKAROUND: Let the statement that modifies the constant value or constraint delay execution! Even if it is 0.1 seconds, you can make the storyboard after the initial completion of the corresponding constraint, so that it will not be overwritten!
Specific methods: See 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 performselector: @selector (modifyconstant) withobject:nil afterdelay:0.1];//lazy load, execute <pre name= "code" class= " CPP "style=" FONT-SIZE:14PX; line-height:26px; " >modifyconstant,0.1 seconds before changing the constraint value!
}-(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, welcome to explore together! Original Address Http://blog.csdn.net/yangbingbinga
IOS AutoLayout Automatic Layout Intermediate Development tutorial (5)-Modify the value of the constraint, defer loading