【问题标题】:How to add label programmatically and position it with auto layout programmatically如何以编程方式添加标签并以编程方式使用自动布局定位它
【发布时间】:2014-03-25 15:00:33
【问题描述】:

我正在尝试向 UIScrollView 添加标签。我正在添加一个顶部约束和一个前导约束。 标签没有出现。

这是我的代码。

   self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.label.translatesAutoresizingMaskIntoConstraints = NO;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.textColor=[UIColor whiteColor];
    self.label.text = @"Hello";
    [self.imageCarouselScrollView addSubview:self.label];
    [self bringSubviewToFront:self.label];
    self.titleLabelTopConstraint = [NSLayoutConstraint
                                    constraintWithItem:self
                                    attribute:NSLayoutAttributeTop
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:self.imageCarouselScrollView
                                    attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                    constant:20];
    NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint
                                                  constraintWithItem:self
                                                  attribute:NSLayoutAttributeLeading
                                                  relatedBy:NSLayoutRelationEqual
                                                  toItem:self.imageCarouselScrollView
                                                  attribute:NSLayoutAttributeTrailing
                                                  multiplier:1.0
                                                  constant:20];
    [self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]];

【问题讨论】:

  • 您是否尝试查看UILabel 是否在未添加约束的情况下显示?而且,不要搞笑..您的视图背景颜色没有设置为白色是吗?

标签: ios uiscrollview uilabel autolayout nslayoutconstraint


【解决方案1】:

您必须在滚动视图的右侧(用于水平滚动)或底部(用于垂直滚动)包含一个约束,以便它可以推断内容大小。

【讨论】:

    【解决方案2】:

    我尝试评论你的代码:

        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; //if 'translatesAutoresizingMaskIntoConstraints=NO' frame not work work anymore. Use CGRectZero instead
        self.label.translatesAutoresizingMaskIntoConstraints = NO;
        self.label.backgroundColor = [UIColor clearColor];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.textColor=[UIColor whiteColor];
        self.label.text = @"Hello";
        [self.imageCarouselScrollView addSubview:self.label]; //Your self.label added on 'self.imageCarouselScrollView' but your constraints added on self
        [self bringSubviewToFront:self.label]; // ??? Why?
        self.titleLabelTopConstraint = [NSLayoutConstraint
                constraintWithItem:self
                         attribute:NSLayoutAttributeTop
                         relatedBy:NSLayoutRelationEqual
                            toItem:self.imageCarouselScrollView
                         attribute:NSLayoutAttributeBottom
                        multiplier:1.0
                          constant:20]; //it constrain means: "self view top edge equal to self.imageCarouselScrollView bottom + offset 20 "
        NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint
                constraintWithItem:self
                         attribute:NSLayoutAttributeLeading
                         relatedBy:NSLayoutRelationEqual
                            toItem:self.imageCarouselScrollView
                         attribute:NSLayoutAttributeTrailing
                        multiplier:1.0
                          constant:20];//it constrain means: "self view Leading edge equal to self.imageCarouselScrollView Trailing + offset 20 "
        [self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]];
    

    现在我有几个问题:

    1. 为什么要限制self.label??因为在 translatesAutoresizingMaskIntoConstraints=no 之后它的帧等于 0;你没有看到标签了。
    2. 你有 2 个约束,其他边呢????
    3. 没问题,理事会,尝试使用 'constraintsWithVisualFormat:options:metrics:views:'

    【讨论】:

    • 使用标签,您只有两个约束来确定它的 x 值和 y 值,然后它的宽度和高度将基于它的内容。我还没有真正尝试过 constraintWithVisualFormat。
    • @user1898829 sizeToFits 不能更多地使用自动布局,学习 setContentHuggingPriority:forAxis:setContentCompressionResistancePriority:forAxis:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多