Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell systemLayoutSizeFittingSize adds extra 0.5 px

I have cell with only one button in it, I've added height of button = 30, top = 10 and bottom = 10 constraints, so systemLayoutSizeFittingSize must return height of cell = 50. Nevertheless it returns 50.5 and I see in the log :

Will attempt to recover by breaking constraint 

I'm using grouped table view and separator set to none. Where it takes extra 0.5px?

constraints

code snippet:

[cell layoutSubviews];
return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;// 50;

log:

 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:0x7ffa71e273e0 V:[UIButton:0x7ffa71e24900'Book a new hotel'(30)]>",
"<NSLayoutConstraint:0x7ffa71e20bc0 V:|-(10)-[UIButton:0x7ffa71e24900'Book a new hotel']   (Names: '|':UITableViewCellContentView:0x7ffa71e06030 )>",
"<NSLayoutConstraint:0x7ffa71e23ae0 UITableViewCellContentView:0x7ffa71e06030.bottomMargin == UIButton:0x7ffa71e24900'Book a new hotel'.bottom + 2>",
"<NSLayoutConstraint:0x7ffa705f6640 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ffa71e06030(50.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ffa71e273e0 V:[UIButton:0x7ffa71e24900'Book a new hotel'(30)]>
like image 638
trickster77777 Avatar asked Oct 20 '22 22:10

trickster77777


1 Answers

iOS 8

If your target is iOS8 then it makes this much easier as table view cells can now be sized automagically by AutoLayout.

To do this just return UITableViewAutomaticDimension in your heightForRow method...

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

Essentially this does what I think you are trying to do with your code snippet.

iOS 7

For iOS 7 I'd set out the constraints differently. Instead of setting the top and bottom constraints I'd just have a "centre vertically" constraint.

Then you can return any number you want to return and the constraints won't break.

You don't get the automatic height thing but the height doesn't appear to change anyway.

like image 53
Fogmeister Avatar answered Nov 04 '22 19:11

Fogmeister