ios - NSLayoutConstraint 在应用程序运行期间什么时候起作用?

标签 ios iphone nslayoutconstraint nsrunloop

我在 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?

viewWillLayoutSubviewsviewDidLayoutSubviews 之间。

关于ios - NSLayoutConstraint 在应用程序运行期间什么时候起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623264/

相关文章:

ios - SwiftUI 演示模式 : Check if view is presented by sheet

iphone - 如何实现uiscrollview的延迟加载

ios - 谷歌地图全屏显示。如何将其填充到半屏?

swift - 对 Superview 边缘的编程自动布局约束

ios - UIView+Autolayout 没有给我正确的定位——我做错了什么?

ios - Spotify SPTAuthViewController 问题

c# - 如何为 iPhone 制作聊天客户端

ios - 添加带有简单动画的 subview 时使用 NSLayoutConstraints

ios - Pod Update 正在删除 "Target Support Files"但没有更新我的项目设置

iphone - 从 sqlite 数据库中查找列中的最大值