[AutoLayout] 代码添加父视图的比例大小高度

Posted baaingSheep

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AutoLayout] 代码添加父视图的比例大小高度相关的知识,希望对你有一定的参考价值。

约束添加到父视图上

意思就是imageView的高度等于self.view的height的0.5倍+0

        self.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0.5, constant: 0))

 

注意:

  

When added to a view, the constraints items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) 

 

  这个方法需要在hierarchy 也就是整个的层级关系组合完成之后使用

  在viewDidLoad(:_)方法中写的时候会出错 

  在viewDidAppear(:_)方法正常使用

 

以上是关于[AutoLayout] 代码添加父视图的比例大小高度的主要内容,如果未能解决你的问题,请参考以下文章

使用 AutoLayout 并确保父视图的边界在调整大小时适合子视图

iOS Autolayout:调整大小父视图中的 UILabels 问题

以 Autolayout 视觉形式调整子视图的大小

添加到父视图时向 UITableView 添加 AutoLayout 约束

如何使用 AutoLayout 根据父高度调整子视图高度而不添加/删除约束

ios-AutoLayout(自动布局代码控制)简单总结