繁体   English   中英

相对于同级UIView的iOS自动布局

[英]iOS Autolayout relative to the sibling UIViews programatically

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

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

在此处输入图片说明

我正在使用自动版式执行此操作。 这是我执行此操作的代码。 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。 一旦我运行上面的代码,我得到以下结果。

在此处输入图片说明

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

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

- (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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM