以编程方式添加约束

时间:2018-11-06 08:42:46

标签: ios objective-c constraints

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

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 .:我不使用界面生成器,所以这不是一个选择。 :)

1 个答案:

答案 0 :(得分:1)

首先,您从未将titleLabel添加为子视图。

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

第三,您正在向视图添加高度限制,而高度限制属于标签。

这是有效的代码:

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

UPD: 在viewDidLoad中向视图控制器添加自定义视图:

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