Reputation: 3026
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = -200;
}
}];
In Objective-C, I would be able to enumerate a view's constraints and adjust the constraints accordingly, but in Swift I'm having difficulty figuring out to do the equivalent.
here is my attempt at applying the code in swift:
for (index, value) in enumerate(view.constraints()) {
var constraint = value as NSLayoutConstraint
if value.firstItem? = view {
constraint.constant = -200;
}
}
I get a compiler error stating "Type '[AnyObject!' does not conform to protocol 'Sequence' on the first line of this code.
Any help would be appreciated!
Upvotes: 2
Views: 1632
Reputation: 37189
As constraints()
returns [AnyObject]!
which is optional so you need to unwrap view.constraints()!
before use.So unwrap it Use below code
for (index, value) in enumerate(view.constraints()!) {
var constraint = value as NSLayoutConstraint
if value.firstItem? = view {
constraint.constant = -200;
}
}
Also you cannot assign firstItem
as it is readonly property.I think you want to compare it.So use if value.firstItem! as UIView == self.view
.So use `
for (index, value) in enumerate(view.constraints()!) {
var constraint = value as NSLayoutConstraint
if value.firstItem! as UIView == self.view {
constraint.constant = -200;
}
}
Upvotes: 2