以编程方式相对于兄弟 UIView 的 iOS 自动布局

标签 ios layout uiview autolayout nslayoutconstraint

我正在尝试在容器 View (C1) 中添加 3 个自定义 View (redView、greenView、yellowView),以便所有自定义 View (redView、greenView、yellowView)以编程方式使用自动布局约束彼此下方。我希望容器 View (C1) 的大小与其 subview 的大小相同,因此输出应该是这样的。 enter image description here

红色、绿色和黄色 View 只是为了显示预期结果。实际上我的自定义 View 是这样的。

enter image description here

我正在使用自动布局来执行此操作。这是我执行此操作的代码。 RatingsSingleView 是我的自定义 View ,如上图所示。

    @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

所以问题是我没有得到预期的结果。我将容器 View 固定在左右边缘,并在 Storyboard 中将其高度设置为 0。运行上面的代码后,我得到以下结果。

enter image description here

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

最佳答案

你给出了一些错误的约束,我已经改正了试试这个...

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

}

关于以编程方式相对于兄弟 UIView 的 iOS 自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912498/

相关文章:

ios - 如何针对特定目标文件大小优化图像大小?

ios - App first Run 和 NSUserDefaults,它让我眼花缭乱。

iphone - 以编程方式创建 UISwitch

layout - JS 的重要性帮助我决定使用或不使用模板布局模块 (css3)

ios - 调整 subview 大小时出错

objective-c - [ self 自动释放] 可以接受吗?

ios/xcode : Possible to send notification when app is in foreground (as alternative to alert)

ios - ld : 1 duplicate symbol for architecture arm64 : two same variables in two different classes?

java - 您推荐哪种 Swing 布局?

python - 如何使用 pack 或 grid 实现以下 Tkinter GUI 布局?