UITableviewCell 中的 UICollectionView。添加约束后 UICollectionView 不显示

UICollectionView within UITableviewCell. UICollectionView not showing once constraints are added

所以我遇到了一个奇怪的错误。我在 UITableView 中有一个 UICollectionView,只要我不添加约束就会显示。我试图使用一个简单的约束将 collectionView 限制在单元格的左侧,但是每当我添加约束时,视图都会消失。我没有得到任何自动布局 bugs/warnings,事实上调试器不会抛出任何类型的标志。但是我仍然无法让 collectionView 在添加约束时显示。 collectionView 的左侧应该有一个标签。如何让两者都出现在单元格中?

代码如下:

    // Init the average usage label
    self.labelMonthlyUsage = [[UILabel alloc] init];

    // Setup the average usage label
    self.labelMonthlyUsage.userInteractionEnabled = NO;
    self.labelMonthlyUsage.text = @"Monthly Usage:";
    self.labelMonthlyUsage.adjustsFontSizeToFitWidth = YES;
    self.labelMonthlyUsage.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelMonthlyUsage.hidden = NO;
    self.labelMonthlyUsage.numberOfLines = 1;
    self.labelMonthlyUsage.textColor = [UIColor blackColor];
    self.labelMonthlyUsage.textAlignment = NSTextAlignmentLeft;

    // Init the collection view
    EPSCVFlowLayout *layout = [[EPSCVFlowLayout alloc] init];
    self.cvMonthlyUsage = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 190, 150) collectionViewLayout:layout];
    [self.cvMonthlyUsage registerClass:[EPSCollectionViewCell class] forCellWithReuseIdentifier:@"cvCell"];
    self.cvMonthlyUsage.delegate = self;
    self.cvMonthlyUsage.dataSource = self;
    self.cvMonthlyUsage.backgroundColor = [UIColor greenColor];
    self.cvMonthlyUsage.translatesAutoresizingMaskIntoConstraints = NO;
    self.cvMonthlyUsage.hidden = NO;
    self.cvMonthlyUsage.userInteractionEnabled = YES;
    self.cvMonthlyUsage.clipsToBounds = YES;
    self.cvMonthlyUsage.scrollEnabled = YES;
    [self.cvMonthlyUsage setNeedsLayout];
    [self.cvMonthlyUsage layoutIfNeeded];

    // Add the views to the cell
    [self addSubview:self.labelMonthlyUsage];
    [self addSubview:self.cvMonthlyUsage];

    // Add constraints for the views in the cell
    NSLayoutConstraint *labelConstraint = [NSLayoutConstraint constraintWithItem:self.labelMonthlyUsage
                                                                           attribute:NSLayoutAttributeCenterY
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self
                                                                           attribute:NSLayoutAttributeCenterY
                                                                          multiplier:1
                                                                            constant:0];

    NSDictionary *views = [NSDictionary dictionaryWithObjects:@[self.labelMonthlyUsage, self.cvMonthlyUsage]
                                                          forKeys:@[@"label", @"cv"]];

      NSArray *viewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[label]-(>=8,<=300)-[cv]-10-|"
                                                                           options:NSLayoutFormatAlignAllCenterY
                                                                           metrics:nil
                                                                             views:views];

    [self addConstraint:labelConstraint];
    [self addConstraint:viewConstraints];

在此先感谢您的帮助!

感谢 Phillip Mills 帮助我解决了这个问题。当我访问视图调试器时,它显示 UICollectionView 对位置和大小有不明确的约束。添加一些新约束后,一切正常。感谢您拯救这一天!