iOS 8 - 查找具有标识符的 UIView 的约束
iOS 8 - Find constraints for a UIView with identifier
我正在尝试在 viewDidLoad:
中为 iPhone 4S,5,5S 更改 ImageView 的约束
for (NSLayoutConstraint *constraint in logoImage.constraints) {
if ([constraint.identifier isEqualToString:@"logoTopIdentifier"]) {
constraint.constant=10;
}
}
好像连循环都不迭代了。有没有其他方法可以通过标识符获得特定约束?
您可以将情节提要中的约束连接到视图控制器 class 与连接 UI 元素相同。
只需在故事板中找到约束,将工作区设为拆分视图,然后将约束拖到相应的视图控制器中class。
有时如果你想为位置变化设置动画,你可以像这样更新约束:
self.theConstraint?.constant = 100
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7) { () -> Void in
self.view.layoutIfNeeded()
}
阻止。
就是这样。
这是 KVConstraintExtensionsMaster
库,您可以通过它从基于 NSLayoutAttribute
的 view
访问任何常量。无论该约束是添加 Programmatically
还是来自 Interface Builder
.
[self.logoImage accessAppliedConstraintByAttribute:NSLayoutAttributeTop completion:^(NSLayoutConstraint *expectedConstraint){
if (expectedConstraint) {
expectedConstraint.constant = 10;
/* for the animation */
[self.logoImage updateModifyConstraintsWithAnimation:NULL];
}
}];
我正在尝试在 viewDidLoad:
中为 iPhone 4S,5,5S 更改 ImageView 的约束for (NSLayoutConstraint *constraint in logoImage.constraints) {
if ([constraint.identifier isEqualToString:@"logoTopIdentifier"]) {
constraint.constant=10;
}
}
好像连循环都不迭代了。有没有其他方法可以通过标识符获得特定约束?
您可以将情节提要中的约束连接到视图控制器 class 与连接 UI 元素相同。
只需在故事板中找到约束,将工作区设为拆分视图,然后将约束拖到相应的视图控制器中class。
有时如果你想为位置变化设置动画,你可以像这样更新约束:
self.theConstraint?.constant = 100
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7) { () -> Void in
self.view.layoutIfNeeded()
}
阻止。
就是这样。
这是 KVConstraintExtensionsMaster
库,您可以通过它从基于 NSLayoutAttribute
的 view
访问任何常量。无论该约束是添加 Programmatically
还是来自 Interface Builder
.
[self.logoImage accessAppliedConstraintByAttribute:NSLayoutAttributeTop completion:^(NSLayoutConstraint *expectedConstraint){
if (expectedConstraint) {
expectedConstraint.constant = 10;
/* for the animation */
[self.logoImage updateModifyConstraintsWithAnimation:NULL];
}
}];