gpt4 book ai didi

ios - 如何以编程方式创建布局约束

转载 作者:IT王子 更新时间:2023-10-29 07:36:25 24 4
gpt4 key购买 nike

我在通用应用程序的底部显示一个 View ,并在我的 View 中动态添加该 View 。我想每次都像 iAd 一样在底部显示这个 View 。在两个方向。我该如何为此添加约束。请提出建议。

谢谢

最佳答案

要将 View 固定到屏幕底部,您需要设置以下约束。

  1. Leading Constraint 关于父 View 的 - X
  2. Trailing Constraint 相对于父 View 的 - Width
  3. Bottom Constraint 关于父 View 的 - Y
  4. Height Constraint 附加到自身 - Height

让我们添加。

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];

希望这对您有所帮助。

干杯。

关于ios - 如何以编程方式创建布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31651022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com