簡體   English   中英

將UIImageView引腳固定到其Superview的頂部,左側,右側,底部,等寬和等高的編程方式是什么?

[英]What's the programmatic way to make a UIImageView pin to it's superview's top, left, right, bottom, equal width, and equal height?

我正在嘗試了解自動布局如何以編程方式工作。 我知道我們可以在Interface Builder中對其進行分配,但我想進一步了解其在下面的工作方式。

如果有人可以向我展示制作UIImageView引腳的代碼,那么它的頂部,左側,右側和底部對齊(以及相等的寬度和相等的高度)將很棒!

yourView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = NSDictionaryOfVariableBindings(yourView);

[superview addConstraints:
 [NSLayoutConstraint
  constraintsWithVisualFormat:@"H:|[yourView]|"
  options:(NSLayoutFormatAlignAllLeading | NSLayoutFormatAlignAllTrailing)
  metrics:nil
  views:views]];

[superview addConstraints:
 [NSLayoutConstraint
  constraintsWithVisualFormat:@"V:|[yourView]|"
  options:(NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom)
  metrics:nil
  views:views]];`

您可以使用本地NSLayoutConstraints來應用約束,如下所示:

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[superview addConstraints:@[

    //view1 constraints
    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0
                                  constant:padding.left],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0
                                  constant:-padding.bottom],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeRight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeRight
                                multiplier:1
                                  constant:-padding.right],

 ]];

對於初學者來說這將是非常復雜的,只需幾行代碼即可實現的一種簡單方法是使用自動布局包裝器(例如Masonry)

使用石工,您可以執行上述操作:

這是使用MASConstraintMaker創建的相同約束

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

甚至更短

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM