iOS 8 - 使用标识符查找UIView的约束

Bla*_*ckM 7 constraints objective-c ios autolayout adaptive-layout

我正在尝试在viewDidLoad中为iPhone 4S,5,5S更改ImageView的约束:

for (NSLayoutConstraint *constraint in logoImage.constraints) {
    if ([constraint.identifier isEqualToString:@"logoTopIdentifier"]) {
        constraint.constant=10;
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎它甚至不在循环中迭代.有没有其他方法来获得标识符的特定约束?

Sur*_*ely 4

您可以将 Storyboard 中的约束连接到视图控制器类,就像连接 UI 元素一样。

只需在故事板中找到约束,将工作区设置为分割视图,然后将约束拖动到相应的视图控制器类即可。

有时,如果您想要为位置变化设置动画,您可以更新约束,例如:

self.theConstraint?.constant = 100 
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7) { () -> Void in
    self.view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)

堵塞。

这就对了。