2017-05-09 106 views
0

所以我遇到了一個奇怪的錯誤。我在UITableView內部有一個UICollectionView,它顯示我何時不添加約束。我試圖使用一個簡單的約束將collectionView限制在單元格的左側,但是每當添加約束時視圖就會消失。我沒有得到任何自動佈局錯誤/警告,實際上調試器不會拋出任何標誌。但是仍然無法在添加約束時顯示collectionView。應該在collectionView的左邊有一個標籤。我怎樣才能讓他們出現在牢房裏?UITableviewCell中的UICollectionView。 UICollectionView沒有顯示一次約束被添加

的代碼如下:提前爲幫助

// 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

嘗試使用Xcode的視圖調試功能。它會告訴你你的視圖在屏幕上的位置以及原因。您也可以告訴它顯示剪輯視圖,這常常解釋爲什麼它們不在屏幕的可見區域。 –

+0

剛纔你說了什麼,發現儘管設置了約束條件,但是位置,大小和可滾動內容約束都是不明確的。現在一旦我添加了正確的約束條件,它就會正常顯示。感謝您的建議! – trigonoah

回答

0

感謝Phillip Mills幫我解決問題。當我訪問視圖調試器時,它顯示UICollectionView對位置和大小有不明確的約束。添加一些新的約束條件後,一切都很好。謝謝你節省了一天!

相關問題