How do I modify the value of a AutoLayout constraint?
| 123456 |
< Code class= "Java plain" > Now I know of a method that has 5 1 2 float Value 3 4 1 =Y&NBSP; 5 remove constraint (remove at runtime), add new constraint |
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!
The result of the original 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:
| 1234567891011121314151617181920212223242526272829 |
//// ViewController.m// SizeClass//// Created by http://blog.csdn.net/yangbingbinga 15/1/21.// Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga All rights reserved.//#importViewController.h@interface ViewController ()@property(strong, nonatomic) IBOutlet NSLayoutConstraint *top;@property(strong, nonatomic) IBOutlet NSLayoutConstraint *height;@end@implementationViewController- (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:
| 123456789101112131415161718192021222324252627282930 |
//// ViewController.m// SizeClass//// Created by http://blog.csdn.net/yangbingbinga 15/1/21.// Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga All rights reserved.//#importViewController.h@interfaceViewController ()@property(strong, nonatomic) IBOutlet NSLayoutConstraint *top;@property(strong, nonatomic) IBOutlet NSLayoutConstraint *height;@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad]; 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
| 1 |
NSLayoutConstraint的对象,都有 constant 值,我们 拖拽到代码中可以直接改变其值,也不会产生任何警告和冲突! |
| 1 |
但是,这还没完,你在开发时可能会遇到,在 viewDidLoad中修改 过 constant值,或者修改其他约束的值,却没有产生任何效果: |
| 1 |
这是因为 :你在storyboard设置的约束是这样处理的 : 你在 viewDidLoad中修改的约束的代码块运行了,但是 运行完之后 又被 storyboard自己的配置给覆盖了,所以 你看到的还是你之前设置的约束! |
| 1 |
解决办法: 让修改 constant值或者约束的语句延迟执行即可!即使是 0.1秒,也能让 在storyboard初始完成之后你再修改相应的约束,这样就不会被覆盖掉了! |
| 12345678910111213141516 |
//// ViewController.m// SizeClass//// Created by http://blog.csdn.net/yangbingbinga 15/1/21.// Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga All rights reserved.#importViewController.h@interfaceViewController ()@property(strong, nonatomic) IBOutlet NSLayoutConstraint *top;@property(strong, nonatomic) IBOutlet NSLayoutConstraint *height;@end@implementationViewController- (void)viewDidLoad{ [superviewDidLoad]; [self performSelector:@selector(modifyConstant) withObject:nil afterDelay:0.1];//延迟加载,执行 |
- 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
reprinted from:A little drowning fish.
iOS xib and code frame layouts Iosxib The values of code modification constraints after layout