我试图在容器视图(C1)内添加3个自定义视图(redView,greenView,yellowView),以使所有自定义视图(redView,greenView,yellowView)都使用自动布局约束以编程方式位于彼此下方。我希望容器视图(C1)的大小与其子视图的大小相同,因此输出应如下所示。
ios - iOS Autolayout相对于同级UIView的编程方式-LMLPHP

红色,绿色和黄色视图仅用于显示预期结果。其实我的自定义视图就是这样。

ios - iOS Autolayout相对于同级UIView的编程方式-LMLPHP

我正在使用自动版式执行此操作。这是我执行此操作的代码。 RatingsSingleView是我的自定义视图,如上图所示。

    @interface ViewController ()

    @property (weak, nonatomic) IBOutlet UIView *ratingsContainerView;

    @end

    @implementation ViewController



        - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *previousTopView = self.ratingsContainerView;
    for(int i = 0; i < 3; ++i) {
        RatingsSingleView *view = [[RatingsSingleView alloc] init];
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.ratingsContainerView addSubview:view];
        NSLayoutConstraint *topConstraint = nil;
        if(i == 0) {
            // Making the first subview top aligned to the container View top
            topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTopView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0];
        } else{
                    // Making the second and third subview top aligned to the view above it
            topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTopView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10.0];
        }

        NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.ratingsContainerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.0];

        NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.ratingsContainerView attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.0];


        [self.ratingsContainerView addConstraint:topConstraint];
        [self.ratingsContainerView addConstraint:leftConstraint];
        [self.ratingsContainerView addConstraint:rightConstraint];


        if(i == 2) {
            // Adding last subview bottom to the container View bottom
            NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.ratingsContainerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0];
             [self.ratingsContainerView addConstraint:bottomConstraint];

        }
        previousTopView = view;

    }
}
@end


所以问题是我没有得到预期的结果。我将容器视图固定在左右边缘,并在情节提要中将其高度设置为0。一旦我运行上面的代码,我得到以下结果。

ios - iOS Autolayout相对于同级UIView的编程方式-LMLPHP

有人可以指导我在这里做错什么吗。谢谢

最佳答案

您给了一些错误的约束条件,而我已更正了这个尝试...

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *previousTopView = self.ratingsContainerView;
    for(int i = 0; i < 3; ++i) {
        RatingsSingleView *view = [[RatingsSingleView alloc] init];
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.ratingsContainerView addSubview:view];
        NSLayoutConstraint *topConstraint = nil;
        if(i == 0) {
            // Making the first subview top aligned to the container View top
            topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTopView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0];
        } else{
                // Making the second and third subview top aligned to the view above it
            topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTopView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10.0];
        }

        NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.ratingsContainerView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10.0];

        NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.ratingsContainerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10.0];


        [self.ratingsContainerView addConstraint:topConstraint];
        [self.ratingsContainerView addConstraint:leftConstraint];
        [self.ratingsContainerView addConstraint:rightConstraint];

        if(i == 2) {
            // Adding last subview bottom to the container View bottom
            NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.ratingsContainerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10.0];
            [self.ratingsContainerView addConstraint:bottomConstraint];

        }
        previousTopView = view;

}

10-08 01:01