0

在我看来,我有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 中设置什么)

4

2 回答 2

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
于 2013-10-10T11:35:25.160 回答
0

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

[UIView intrinsicContentSize] 的文档

于 2013-10-10T15:17:40.510 回答