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
NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint constraintWithItem:self. uiButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self. view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self. view addConstraint:centreHorizontallyConstraint];
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.
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.
To fix a view to the bottom of the screen you need following constraints to set.
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.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With