NSLayoutConstraint 在应用程序运行期间何时工作?
When do NSLayoutConstrains work during app runs?
我在故事板中设置了一些 NSLayoutConstrains
。我在 viewDidload
中更改了子视图的框架,它不起作用。我在 viewWillAppear
中更改它,它不起作用。我在 viewDidAppear
中更改它,它有效。所以当我在 storyboard
中设置约束时,
什么时候限制设置子视图的框架?成功后,我可以设置视图框架了。谢谢!
下面是我要制作的动画,我可以给frame和constrains做动画,效果一样,代码是:
框架版本:
在 viewDidload
中,我首先设置初始框架,
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
然后在 viewWillAppear
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - self.containerView.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
结果是containerView从下往上出现。
在互联网上搜索后,我应该更改约束常数而不是框架。所以我将代码更改为:
override func viewDidLoad() {
super.viewDidLoad()
self.bottomConstrain.constant = -self.containerView.frame.size.height
}
和整数
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.bottomConstrain.constant = 0
self.view.layoutIfNeeded()
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
两种方法都是一样的。所以我也想知道它们有什么区别
请尝试以下代码。希望对你有用。
- (void)viewWillLayoutSubviews
{
//set your constant outside the animation
self.YOUR_SUB_VIEW_CONSTRAINT.constant = 0; //As per your requirement whatever constant value you can set.
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
这是用动画改变你的约束。
谢谢:)
So when I set up constrains in storyboard, when do that constrains work to set the subviews' frame?
在 viewWillLayoutSubviews
和 viewDidLayoutSubviews
之间。
我在故事板中设置了一些 NSLayoutConstrains
。我在 viewDidload
中更改了子视图的框架,它不起作用。我在 viewWillAppear
中更改它,它不起作用。我在 viewDidAppear
中更改它,它有效。所以当我在 storyboard
中设置约束时,
什么时候限制设置子视图的框架?成功后,我可以设置视图框架了。谢谢!
下面是我要制作的动画,我可以给frame和constrains做动画,效果一样,代码是:
框架版本:
在 viewDidload
中,我首先设置初始框架,
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
然后在 viewWillAppear
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - self.containerView.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
结果是containerView从下往上出现。 在互联网上搜索后,我应该更改约束常数而不是框架。所以我将代码更改为:
override func viewDidLoad() {
super.viewDidLoad()
self.bottomConstrain.constant = -self.containerView.frame.size.height
}
和整数
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.bottomConstrain.constant = 0
self.view.layoutIfNeeded()
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
两种方法都是一样的。所以我也想知道它们有什么区别
请尝试以下代码。希望对你有用。
- (void)viewWillLayoutSubviews
{
//set your constant outside the animation
self.YOUR_SUB_VIEW_CONSTRAINT.constant = 0; //As per your requirement whatever constant value you can set.
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
这是用动画改变你的约束。
谢谢:)
So when I set up constrains in storyboard, when do that constrains work to set the subviews' frame?
在 viewWillLayoutSubviews
和 viewDidLayoutSubviews
之间。