gpt4 book ai didi

ios - 以编程方式添加约束

转载 作者:行者123 更新时间:2023-11-28 23:42:20 27 4
gpt4 key购买 nike

我正在尝试以编程方式实现以下布局:

enter image description here

它是一个子类的 UIView,我想在它上面放置一个固定高度(40 像素)、动态宽度(宽度是根据文本的长度计算的,所以我想这可以被认为是固定的 UILabel,而不是动态的,因为我只计算了一次),并且距离左侧和底部正好 40 像素。

这是我的代码:

- (UIView *) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{

marginGeneral = 40.0f;

UILabel *titleLabel = [[UILabel alloc] init];
[titleLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
titleLabel.backgroundColor = [UIColor blackColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = @"Some Text";
[self addSubview:titleLabel];

NSLayoutConstraint *contsTitleLeft = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeLeft
multiplier: 1.0
constant: marginGeneral];

NSLayoutConstraint *contsTitleRight = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeRight
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeRight
multiplier: 1.0
constant: marginGeneral * -1.0f];

NSLayoutConstraint *contsTitleBottom = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeBottom
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeBottom
multiplier: 1.0
constant: marginGeneral * -1.0f];

NSLayoutConstraint *contsTitleHeight = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 1.0
constant: 40.0f];

[self addConstraints:@[contsTitleLeft, contsTitleRight, contsTitleBottom]];
[titleLabel addConstraint:contsTitleHeight];

}

return self;
}

@end

这当然会返回各种与约束相关的警告,并且标签不会显示。

你们好心人能帮助我了解我哪里出了问题以及如何纠正吗?非常感谢!

P.s.:我不使用界面生成器,所以这不是一个选项。 :)

最佳答案

首先,您从未将 titleLabel 添加为 subview 。

其次,您需要忽略自动调整大小掩码以避免约束冲突。

第三,您正在为 View 添加高度约束,而它属于标签。

这是有效的代码:

UILabel *titleLabel = [[UILabel alloc] init];
[titleLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
titleLabel.backgroundColor = [UIColor blackColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = @"Some Text";
[self.view addSubview: titleLabel];

NSLayoutConstraint *contsTitleLeft = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeLeft
multiplier: 1.0
constant: marginGeneral];

NSLayoutConstraint *contsTitleRight = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeRight
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeRight
multiplier: 1.0
constant: marginGeneral * -1.0f];

NSLayoutConstraint *contsTitleBottom = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeBottom
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeBottom
multiplier: 1.0
constant: marginGeneral * -1.0f];

NSLayoutConstraint *contsTitleHeight = [NSLayoutConstraint
constraintWithItem: titleLabel
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 1.0
constant: 40.0f];

[self addConstraints:@[contsTitleLeft, contsTitleRight, contsTitleBottom]];
[titleLabel addConstraint: contsTitleHeight];

更新:在 viewDidLoad 中添加自定义 View 到 View Controller :

CustomView *customView = [[CustomView alloc] initWithFrame: CGRectMake(0, 0, 200, 200)];
[customView setTranslatesAutoresizingMaskIntoConstraints: NO];
[customView setBackgroundColor: [UIColor greenColor]];
[self.view addSubview: customView];

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem: customView
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier:1
constant:200];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem: customView
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier:1
constant:200];

[customView addConstraints: @[widthConstraint, heightConstraint]];

NSLayoutConstraint *centerHorizontallyConstraint = [NSLayoutConstraint constraintWithItem: customView
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeCenterX
multiplier: 1
constant: 0];

NSLayoutConstraint *centerVerticallyConstraint = [NSLayoutConstraint constraintWithItem: customView
attribute: NSLayoutAttributeCenterY
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeCenterY
multiplier: 1
constant: 0];

[self.view addConstraints: @[centerHorizontallyConstraint, centerVerticallyConstraint]];

关于ios - 以编程方式添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53168407/

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