在UICollectionViewCell的情况下无法同时满足约束

时间:2014-11-26 11:08:04

标签: ios autolayout nslayoutconstraint icarousel

我正在使用iCarousel来实现封面流程。我在iCarousel数据源方法中提供了它的视图,如下所示:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index   reusingView:(UIView *)view
{

    if(!view) {
        view = [[SDHotOnThumbView alloc]initWithFrame:CGRectMake(0, 0,  kHotOnThumbViewWidth, kHotOnThumbViewHeight)];
        view.contentMode = UIViewContentModeScaleAspectFit;
    }
    [((SDHotOnThumbView*)view) setData:[self.dataArray objectAtIndex:index]];
    return view;
}

方法setData除了在SDHotOnThumbView实例中设置标签文本,并为其中的图像视图开始图像下载外,还调用方法updateLabelLayoutWithAttributedText,该方法在SDHotOnThumbView类中实现:

-(void)updateLabelLayoutWithAttributedText:(NSMutableAttributedString*) labelString
{
    float height = [self heightOfAttributedText:labelString  width:self.frame.size.width];
    self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    //self.descriptionLabel.preferredMaxLayoutWidth = self.frame.size.width;
    NSDictionary *viewsDict = @{@"desclabel":self.descriptionLabel};
    NSArray *constraintsH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0- [desclabel]-0-|" options:0 metrics:nil views:viewsDict];
    [self addConstraints:constraintsH];
    NSArray *constraintsV = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[desclabel(==%f)]-10-|", height]
                                                                options:0
                                                                metrics:nil
                                                                  views:viewsDict];
    [self addConstraints:constraintsV];

}

iCarousel回收视图,如果循环视图为零,我必须分配一个视图,并在每次轮播要求给定索引的视图时配置它。根据属性字符串的大小,我在附加的第二个方法中使用新高度更新标签的垂直约束,以便根据标签包含的数据调整标签大小。视图布局很好,除了我得到。例如,我得到

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.     Try this: (1) look at each constraint and try to figure out which you don't expect; (2)  find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7b82f330 V:[UILabel:0x7d861820'SanDisk Cruzer Blade USB  ...'(51.2)]>",
    "<NSLayoutConstraint:0x7d862df0 V:[UILabel:0x7d861820'SanDisk Cruzer Blade USB ...'(65.6)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7d862df0 V:[UILabel:0x7d861820'SanDisk Cruzer Blade USB  ...'(65.6)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in  the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in
<UIKit/UIView.h> may also be helpful.

记录标签的计算高度,我可以看到,当从集合视图中删除单元格并在集合视图中为某些不同的索引重新添加时,它可能会保持其旧的高度,并尝试添加一个新的垂直约束,打破了前一个约束。我甚至尝试从视图中删除所有标签约束,并且每次调用时都会调用添加的数据,但我在Xcode中看到了同样的情况,即使一切看起来都按预期工作。我如何处理这些Xcode警告,或者如果布局符合要求则没问题。

1 个答案:

答案 0 :(得分:0)

不知怎的,你加倍地在标签上插入2个不同的高度,第一个高度为51.2,第二个高度为65.6。请再次检查您的代码。