UITableviewCell中的UICollectionView。 UICollectionView不会显示一次约束

时间:2017-05-09 16:03:24

标签: ios objective-c uitableview uicollectionview ios-autolayout

所以我遇到了一个奇怪的错误。我在UICollectionView内有UITableView,只要我不添加约束就会显示。我正在尝试使用简单的约束将collectionView限制在单元格的左侧,但每当我添加约束时,视图都会消失。我没有得到任何自动布局错误/警告,实际上调试器不会抛出任何类型的标志。但是我仍然无法在添加约束时显示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];

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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