How to Create layout constraints programmatically

前端 未结 5 618
-上瘾入骨i
-上瘾入骨i 2020-11-27 02:41

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 bot

5条回答
  •  温柔的废话
    2020-11-27 03:10

    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.

提交回复
热议问题