我在 Storyboard中设置了一些 NSLayoutConstrains
。我在 viewDidload
中更改了 subview 的框架,但它不起作用。我在 viewWillAppear
中更改它,它不起作用。我在 viewDidAppear
中更改了它,它起作用了。所以当我在 storyboard
中设置约束时,
什么时候限制设置 subview 的框架?成功后,我可以设置 View 框架了。谢谢!
这是我要制作的动画,我可以为框架和约束设置动画,它显示相同,代码是:
框架版本:
在 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)
这两种方法都是一样的。所以我也想知道它们之间有什么区别。
最佳答案
So when I set up constrains in storyboard, when do that constrains work to set the subviews' frame?
在 viewWillLayoutSubviews
和 viewDidLayoutSubviews
之间。
关于ios - NSLayoutConstraint 在应用程序运行期间什么时候起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623264/