【发布时间】:2015-11-10 05:07:07
【问题描述】:
我在情节提要中设置了一些NSLayoutConstrains。我在viewDidload 中更改了子视图的框架,它不起作用。我在viewWillAppear 中更改它,它不起作用。我在viewDidAppear 中更改它,它可以工作。所以当我在storyboard 中设置约束时,
什么时候限制设置子视图的框架?成功后,我可以设置视图框架。谢谢!
这是我想要制作的动画,我可以为框架和约束设置动画,它显示相同,代码是:
框架版本:
在viewDidload,我先设置了init frame,
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)
这两种方法都是一样的。所以我也想知道它们之间有什么区别。
【问题讨论】:
-
如果子视图受到限制,切勿尝试手动更改其
frame。 -
我知道,我应该改变约束常数,但我改变了框架,它也可以。我想改变框架的原因是我想让动画结合视图背景颜色,我可以在 UIView.animaton(_,duration:,completion) 中做到这一点。但是如果我单独更改约束,我不知道如何做那个动画
标签: ios iphone nslayoutconstraint nsrunloop