How to Create layout constraints programmatically How to Create layout constraints programmatically ios ios

How to Create layout constraints programmatically


To fix a view to the bottom of the screen you need following constraints to set.

  1. Leading Constraint with respect of Parent View for - X
  2. Trailing Constraint with respect of Parent View for - Width
  3. Bottom Constraint with respect of Parent View for - Y
  4. Height Constraint attached to self for - Height.

Lets add.

UIView *subView=bottomView;UIView *parent=self.view;subView.translatesAutoresizingMaskIntoConstraints = NO;//Trailing    NSLayoutConstraint *trailing =[NSLayoutConstraint                                constraintWithItem:subView                                attribute:NSLayoutAttributeTrailing                                relatedBy:NSLayoutRelationEqual                                toItem:parent                                   attribute:NSLayoutAttributeTrailing                                multiplier:1.0f                                constant:0.f];//LeadingNSLayoutConstraint *leading = [NSLayoutConstraint                                   constraintWithItem:subView                                   attribute:NSLayoutAttributeLeading                                   relatedBy:NSLayoutRelationEqual                                   toItem:parent                                   attribute:NSLayoutAttributeLeading                                   multiplier:1.0f                                   constant:0.f];//BottomNSLayoutConstraint *bottom =[NSLayoutConstraint                                 constraintWithItem:subView                                 attribute:NSLayoutAttributeBottom                                 relatedBy:NSLayoutRelationEqual                                 toItem:parent                                 attribute:NSLayoutAttributeBottom                                 multiplier:1.0f                                 constant:0.f];//Height to be fixed for SubView same as AdHeightNSLayoutConstraint *height = [NSLayoutConstraint                               constraintWithItem:subView                               attribute:NSLayoutAttributeHeight                               relatedBy:NSLayoutRelationEqual                               toItem:nil                               attribute:NSLayoutAttributeNotAnAttribute                               multiplier:0                               constant:ADHeight];    //Add constraints to the Parent    [parent addConstraint:trailing];    [parent addConstraint:bottom];    [parent addConstraint:leading];    //Add height constraint to the subview, as subview owns it.    [subView addConstraint:height];

Hope this helps.

Cheers.


Small extension for previous answer because addConstraint will be deprecated in future. Here is an extension for UI view. Use these functions after you added view to hierarchy.

OBJC

@implementation UIView (Constraints)-(void)addConstaintsToSuperviewWithLeftOffset:(CGFloat)leftOffset topOffset:(CGFloat)topOffset {    self.translatesAutoresizingMaskIntoConstraints = false;    [[NSLayoutConstraint constraintWithItem: self                                  attribute: NSLayoutAttributeLeading                                  relatedBy: NSLayoutRelationEqual                                     toItem: self.superview                                  attribute: NSLayoutAttributeLeading                                 multiplier: 1                                   constant: leftOffset] setActive:true];    [[NSLayoutConstraint constraintWithItem: self                                  attribute: NSLayoutAttributeTop                                  relatedBy: NSLayoutRelationEqual                                     toItem: self.superview                                  attribute: NSLayoutAttributeTop                                 multiplier: 1                                   constant: topOffset] setActive:true];}-(void)addConstaintsWithWidth:(CGFloat)width height:(CGFloat)height {    self.translatesAutoresizingMaskIntoConstraints = false;    [[NSLayoutConstraint constraintWithItem: self                                  attribute: NSLayoutAttributeWidth                                  relatedBy: NSLayoutRelationEqual                                     toItem: nil                                  attribute: NSLayoutAttributeNotAnAttribute                                 multiplier: 1                                   constant: width] setActive:true];    [[NSLayoutConstraint constraintWithItem: self                                  attribute: NSLayoutAttributeHeight                                  relatedBy: NSLayoutRelationEqual                                     toItem: nil                                  attribute: NSLayoutAttributeNotAnAttribute                                 multiplier: 1                                   constant: height] setActive:true];}@end

Swift 3

extension UIView {    public func addConstaintsToSuperview(leftOffset: CGFloat, topOffset: CGFloat) {        self.translatesAutoresizingMaskIntoConstraints = false        NSLayoutConstraint(item: self,                           attribute: .leading,                           relatedBy: .equal,                           toItem: self.superview,                           attribute: .leading,                           multiplier: 1,                           constant: leftOffset).isActive = true        NSLayoutConstraint(item: self,                           attribute: .top,                           relatedBy: .equal,                           toItem: self.superview,                           attribute: .top,                           multiplier: 1,                           constant: topOffset).isActive = true    }    public func addConstaints(height: CGFloat, width: CGFloat) {        self.translatesAutoresizingMaskIntoConstraints = false        NSLayoutConstraint(item: self,                           attribute: .height,                           relatedBy: .equal,                           toItem: nil,                           attribute: .notAnAttribute,                           multiplier: 1,                           constant: height).isActive = true        NSLayoutConstraint(item: self,                           attribute: .width,                           relatedBy: .equal,                           toItem: nil,                           attribute: .notAnAttribute,                           multiplier: 1,                           constant: width).isActive = true    }}


Also since iOS 9 it could be done super simple with anchors:

Swift 3

extension UIView {    func addConstaintsToSuperview(leadingOffset: CGFloat, topOffset: CGFloat) {        guard superview != nil else {            return        }        translatesAutoresizingMaskIntoConstraints = false        leadingAnchor.constraint(equalTo: superview!.leadingAnchor,                                 constant: leadingOffset).isActive = true        topAnchor.constraint(equalTo: superview!.topAnchor,                             constant: topOffset).isActive = true    }    func addConstaints(height: CGFloat, width: CGFloat) {        heightAnchor.constraint(equalToConstant: height).isActive = true        widthAnchor.constraint(equalToConstant: width).isActive = true    }}

OBJC category

@implementation UIView (Constraints)-(void)addConstaintsToSuperviewWithLeadingOffset:(CGFloat)leadingOffset topOffset:(CGFloat)topOffset{    if (self.superview == nil) {        return;    }    self.translatesAutoresizingMaskIntoConstraints = false;    [[self.leadingAnchor constraintEqualToAnchor:self.superview.leadingAnchor                                        constant:leadingOffset] setActive:true];    [[self.topAnchor constraintEqualToAnchor:self.superview.topAnchor                                   constant:topOffset] setActive:true];}-(void)addConstaintsWithHeight:(CGFloat)height width:(CGFloat)width{    [[self.heightAnchor constraintEqualToConstant:height] setActive:true];    [[self.widthAnchor constraintEqualToConstant:width] setActive:true];}@end