Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create layout constraints programmatically

I am displaying a view in the bottom of the universal application and adding this view dynamically in my view. I want to show this view in bottom every time like iAd. in both orientation. How can I add constraints for this. Please suggest.

Thanks

like image 922
Mitesh Khatri Avatar asked Jul 27 '15 10:07

Mitesh Khatri


People also ask

How do I set constraints programmatically in Objective C?

NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint constraintWithItem:self. uiButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self. view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self. view addConstraint:centreHorizontallyConstraint];

How do I add a layout constraint in Xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.

How do you add constraints in Uikit?

To create a constraint between two views, Control-click one of the views and drag to the other. When you release the mouse, Interface Builder displays a HUD menu with a list of possible constraints.


2 Answers

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];  //Leading  NSLayoutConstraint *leading = [NSLayoutConstraint                                    constraintWithItem:subView                                    attribute:NSLayoutAttributeLeading                                    relatedBy:NSLayoutRelationEqual                                    toItem:parent                                    attribute:NSLayoutAttributeLeading                                    multiplier:1.0f                                    constant:0.f];  //Bottom NSLayoutConstraint *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 AdHeight NSLayoutConstraint *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.

like image 166
iphonic Avatar answered Sep 17 '22 06:09

iphonic


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     } } 
like image 34
Alex Shubin Avatar answered Sep 21 '22 06:09

Alex Shubin