NSLayoutConstraint,在表视图单元格中子查看 UIView不起作用



所以我正在使用我在Github上找到的一个项目——https://github.com/kmonaghan/CBPWordPress——并试图处理它。

从本质上讲,我想做的是创造像Facebook这样的应用程序所具有的浮动"卡片"错觉。我过去完成此操作的方法是创建一个子视图 UIView,在将标题标签、作者姓名放入该视图之前,先圆角等。问题是我通常使用情节提要来做到这一点,而此示例以编程方式布置了内容。

在玩弄它之后,我以为我已经想通了。这就是我设置 -(void(updateConstraint 方法的方式:

  • (无效(更新约束{if (!self.constraintUpdate( {

    self.contentView.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CBPLargePostPreviewTableViewCellHeight);
    NSDictionary *views = @{@"postCommentLabel": self.postCommentLabel,
                            @"postDateLabel": self.postDateLabel,
                            @"postImageView": self.postImageView,
                            @"postTitleLabel": self.postTitleLabel,
                            @"roundedBackground": self.roundedBackground};
    NSDictionary *metrics = @{@"padding": @(CBPPadding)};
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(4)-[roundedBackground]-(4)-|"
                                                                                   options:0
                                                                                   metrics:metrics
                                                                                     views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(4)-[roundedBackground]-(4)-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:views]];
    [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(5)-[postTitleLabel]-(5)-[postImageView(150)]-(5)-[postDateLabel]-(5)-|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:views]];
    [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postTitleLabel]-(padding)-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:views]];
    [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postImageView]-(padding)-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:views]];
    [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postDateLabel]-(>=0)-[postCommentLabel]-(padding)-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:views]];
    [self.roundedBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.postCommentLabel
                                                                 attribute:NSLayoutAttributeCenterY
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.postDateLabel
                                                                 attribute:NSLayoutAttributeCenterY
                                                                multiplier:1.0f
                                                                  constant:0]];
    self.constraintsUpdated = YES;
    }
    [super updateConstraints];
    }
    

但是在运行时,我收到此错误:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象[4]插入零对象'

我尝试为视图创建边界,但无济于事。有人有什么建议吗?如果有更好的方法来创造我正在寻找的效果,我也对此持开放态度。

似乎您在视觉格式字符串中使用了 roundedBackground,但有背景输入您的观点 字典。

最新更新