Reputation: 1461
I need to modify leading space of a View . here is my code
let advSearchViewAddLeadingConstraint: NSLayoutConstraint = NSLayoutConstraint(
item:advancedSearchView,
attribute:NSLayoutAttribute.Leading,
relatedBy:NSLayoutRelation.Equal,
toItem:self.view,
attribute:NSLayoutAttribute.NotAnAttribute,
multiplier:0,
constant:200)
self.view.addConstraint(advSearchViewAddLeadingConstraint)
By using this code i am getting crash as
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Unknown layout attribute'
Upvotes: 1
Views: 1835
Reputation: 87
I would work at it the other way around. Create a containing view and drag an IBOutlet for leading space constraint. Then just change the constant, it should work better.
leadingConstraint.constant = 200
self.view.layoutIfNeeded()
Upvotes: 1
Reputation: 8148
You probably want NSLayoutAttribute.Leading
instead of NSLayoutAttribute.NotAnAttribute
. If you're providing two items, you need to specify the attribute of each item:
let advSearchViewAddLeadingConstraint: NSLayoutConstraint =
NSLayoutConstraint(
item:advancedSearchView,
attribute:NSLayoutAttribute.Leading,
relatedBy:NSLayoutRelation.Equal,
toItem:self.view,
attribute:NSLayoutAttribute.Leading,
multiplier:0,
constant:200)
A couple of other notes:
You can activate the constraint with: advSearchViewAddLeadingConstraint.active = true
In Swift, type inference can help you out with enum types, so instead of typing NSLayoutAttribute.Leading
you can just type Leading
.
Upvotes: 0