Hi All,
You can add auto layout constraints by using Storyboard Interface OR by adding constraints dynamically. There are three ways to add constraint dynamically (as per Apple Inc.)
NSLayoutConstraint is one of the way is used to add autolayout constraints dynamically.
NSLayoutConstraint Example :-
let new_view:UIView! = UIView(frame: CGRectMake(20, 20, 100, 100));
new_view.backgroundColor = UIColor.redColor();
new_view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(new_view);
NSLayoutConstraint(item: new_view,
attribute: .Leading,
relatedBy: .Equal,
toItem: view,
attribute: .LeadingMargin,
multiplier: 1.0,
constant: 0.0).active = true
NSLayoutConstraint(item: new_view,
attribute: .Top,
relatedBy: .Equal,
toItem: view,
attribute: .TopMargin,
multiplier: 1.0,
constant: 20.0).active = true
NSLayoutConstraint(item: new_view,
attribute: .Height,
relatedBy: .Equal,
toItem: new_view,
attribute:.Width,
multiplier: 2.0,
constant:0.0).active = true
NSLayoutConstraint(item: new_view,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1.0,
constant: 100.0).active = true
0 Comment(s)