如何在调用setFrame后触发视图自动布局调整?

时间:2014-04-10 22:08:23

标签: cocoa autolayout

假设我必须使用宽度均匀的视图。左右边缘在它们之间垂直对齐5个空间。如果我调用setFrame:在顶视图上将其向上移动到超级视图中,下方视图是否应该自动移动?

enter image description here

1 个答案:

答案 0 :(得分:2)

是的,但如果您使用自动布局来定位上方视图,则不应调用setFrame。那将没有任何效果。 而是,修改约束。 在视图控制器中创建一个插座,引用上部视图和顶部布局指南之间垂直空间的约束。

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalSpaceConstraint;

然后代替setFrame,执行:

verticalSpaceConstraint.constant = 10; // or whatever distance you want between the top of the screen and the upper view