【问题标题】:How to set self view height using constraints?如何使用约束设置本端视图高度?
【发布时间】:2014-09-04 18:14:10
【问题描述】:
所以我一直在移动部分代码以使用约束,而不是使用框架的旧方式。这是我需要移动的当前代码。当我在用户按下按钮时隐藏\取消隐藏下面的标签栏时,我需要更改视图的高度。
CGRect newFrame = self.view.frame;
newFrame.size.height += 44
self.view.frame = newFrame;
self.view 是 UIViewController。我想设置图片中显示的视图的高度。没有办法给它添加高度约束(你可以在子视图上设置它,虽然我不想要)
我没有看到使用约束设置本端视图高度的方法。
谢谢
阿尔皮特
【问题讨论】:
标签:
ios
autolayout
constraints
【解决方案1】:
这篇短文tutorial会对你有所帮助。
您可以使用 VFL(我个人更喜欢,请参阅上面的链接)或通过这种代码:
[yourView addConstraint:[NSLayoutConstraint constraintWithItem:yourView
attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:requiredHeight]];
这段代码将为yourView 添加由requiredHeight 指定的高度约束。在开始编码之前,请先看一下intrinsicContentSize 和updateConstraints 方法。
在设置约束时,请记住首先设置(对于一个视图,你将设置约束)translatesAutoresizingMaskIntoConstraints 标志为 NO(默认设置为 YES)。还阅读this 应该对这个主题有所了解。