如何以编程方式创build布局约束
我在通用应用程序底部显示一个视图,并在我的视图中dynamic添加此视图。 我想每次像iAd一样在底部显示这个视图。 在两个方向。 我怎样才能为此添加约束。 请build议。
谢谢
要将视图固定到屏幕的底部,需要设置以下约束条件。
- 领先的约束关于父视图的X
- 关于父视图的 – 宽度的 追踪约束
- 对于Y的父视图的底线约束
- 身高约束附加到自我 – 身高 。
让我们添加。
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];
希望这可以帮助。
干杯。
以前答案的小扩展名,因为addConstraint
将来会被弃用。 这是UI视图的扩展。 将视图添加到层次结构后使用这些函数。
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 } }
另外从iOS 9开始,可以通过锚来完成超级简单的操作:
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类别
@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