I am using iCarousel to implement a cover flow. I provide views to it in my iCarousel data source method, like this:
- (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;
}
The method setData apart from setting the label's text in SDHotOnThumbView instance, and starting image download for the image view in it, also makes a call to a method updateLabelLayoutWithAttributedText, which is implemented as under in the SDHotOnThumbView class:
-(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 recycles views, and I have to allocate a view if the recycled view is nil, and configure it every time the carousel asks for a view at a given index. Based on the size of an attributed string, I update vertical constraints for the label with the new height, in the second method I have attached, so that the label is resized as per the data it contains. The views layout just fine, except that I get . For example, I get
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.
Logging computed heights for the label, I can see that probably when the cell is removed from the collection view and re-added for some different index in the collection view, it probably holds on to its old height, and tries to add a new vertical constraint, breaking the previous one. I even tried removing all label constraints from the view, and adding everytime set data is called, but i see the same happening in Xcode, even though everything seems to work as expected. How do I go about these Xcode warnings, or is it fine if the layout is as desired.