自动布局:视图高度始终为零

时间:2014-04-16 10:09:18

标签: ios objective-c

我在viewDidLoad中有这样的代码:

UIView *parentView = [[UIView alloc] init];
parentView.translatesAutoresizingMaskIntoConstraints = NO;

UIView *topView = nil;
for (int i = 0; i < 3; i++) {

    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = self.data[i];
    nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [parentView addSubview:nameLabel];

    UIImageView *clockIconView = [[UIImageView alloc] init];
    clockIconView.image = [UIImage imageNamed:@"img"];
    clockIconView.translatesAutoresizingMaskIntoConstraints = NO;
    [parentView addSubview:clockIconView];

    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = self.time;
    timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [parentView addSubview:timeLabel];

    [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel(>=150)]-[clockIconView(20)]-1-[timeLabel(39)]-|"
                                                                           options:NSLayoutFormatAlignAllBaseline
                                                                           metrics:nil
                                                                             views:NSDictionaryOfVariableBindings(nameLabel, clockIconView, timeLabel)]];

    // First view - add constraint to superview
    if (i == 0) {
        [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[nameLabel(20)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:NSDictionaryOfVariableBindings(nameLabel)]];
    }

    // Add constraint to previous view
    if (topView) {
        [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topView]-[nameLabel(20)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:NSDictionaryOfVariableBindings(nameLabel, topView)]];
    }

    // Last view - add constraint to superview's bottom
    if (i == 2) {
        [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel(20)]-|"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:NSDictionaryOfVariableBindings(nameLabel)]];
    }

    [parentView layoutIfNeeded];
    topView = nameLabel;
}

NSLog(@"%@", parentView);

首先,我添加水平约束以对齐标签和imageView。然后,我创建了垂直约束。

但是,parentView的框架具有负原点x,高度始终为0.无法找出原因。高度为零的任何想法?

<UIView: 0x17037b600; frame = (-129 0; 258 0); autoresize = W+H; layer = <CALayer: 0x170036da0>>

0 个答案:

没有答案