Autolayout在堆栈中一个接一个地查看

时间:2013-10-10 11:30:19

标签: objective-c cocoa-touch autolayout

在我看来,我有3个子视图,一个接一个地跟着

------------
|    V1    |
------------
|    V2    |
------------
|    V3    |
------------

现在,V1-V3可以有不同的高度(甚至高度为0),具体取决于内容(我可以计算出所需的高度)

------------
|    V1    |
|    V1    |
|    V1    |
------------
------------
|    V3    |
|    V3    |
------------

我玩了几个小时的IB限制,但是无法正确判断,所以我决定让它成为纯粹的程序化,但我也在约束上遇到问题。

- (void)setupWithContent:(NSDictionary *)content {

    CGFloat width123 = 200.f;

    CGFloat height1 = [content[@"height1"] floatValue];
    CGFloat height2 = [content[@"height2"] floatValue];
    CGFloat height3 = [content[@"height3"] floatValue];

    [self.view addConstraints:...];
    [self.view1 addConstraints:...];
    [self.view2 addConstraints:...];
    [self.view3 addConstraints:...];

}

如何使用程序化约束来实现这一目标(在程序化案例中需要在IB中设置什么)

2 个答案:

答案 0 :(得分:2)

您可以使用可视化格式化语言轻松完成。这是一个例子:

@interface DAViewController ()

@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIView *yellowView;
@property (strong, nonatomic) UIView *purpleView;
@property (strong, nonatomic) UIView *brownView;

@end

@implementation DAViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    self.contentView = [self viewWithColor:[UIColor lightGrayColor]];
    [self.view addSubview:self.contentView];

    self.yellowView = [self viewWithColor:[UIColor yellowColor]];
    [self.contentView addSubview:self.yellowView];

    self.purpleView = [self viewWithColor:[UIColor purpleColor]];
    [self.contentView addSubview:self.purpleView];

    self.brownView = [self viewWithColor:[UIColor brownColor]];
    [self.contentView addSubview:self.brownView];

    NSDictionary *views = @{@"yellowView" : self.yellowView,
                            @"purpleView" : self.purpleView,
                            @"brownView" : self.brownView};

    NSDictionary *metrics = @{@"padding" : @5,
                              @"width123" : @200,
                              @"height1" : @50,
                              @"height2" : @210,
                              @"height3" : @40};

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                          attribute:NSLayoutAttributeCenterY
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeCenterY
                                                         multiplier:1.
                                                           constant:0.]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1.
                                                           constant:0.]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-padding-[yellowView(==height1)][purpleView(==height2)][brownView(==height3)]-padding-|" options:0 metrics:metrics views:views]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[yellowView(==width123)]-padding-|" options:0 metrics:metrics views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[purpleView(==width123)]-padding-|" options:0 metrics:metrics views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[brownView(==width123)]-padding-|" options:0 metrics:metrics views:views]];
}

- (UIView *)viewWithColor:(UIColor *)color
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    view.backgroundColor = color;

    return view;
}

@end

答案 1 :(得分:0)

如果覆盖每个视图上的intrinsicContentSize函数以返回所需的大小,并在所需大小更改时在视图上调用invalidateIntrinsicContentSize,则IB中定义的约束应该按照您希望的方式工作。

Documentation for [UIView intrinsicContentSize]