Masonry icon indicating copy to clipboard operation
Masonry copied to clipboard

You shouldn't touch translatesAutoresizingMaskIntoConstraints on the contentView of UITableViewCell/UICollectionViewCells

Open tzahola opened this issue 8 years ago • 3 comments

New Issue Checklist

🚫 If this template is not filled out your issue will be closed with no comment. 🚫

  • [x] I have looked at the Documentation
  • [x] I have filled out this issue template.

Issue Info

Info Value
Platform ios
Platform Version all
Masonry Version all
Integration Method all

Issue Description

UITableViewCell and UICollectionViewCell manages its contentView manually. In other words, UIKit relies on the cells' contentView having translatesAutoresizingMaskIntoConstraints being YES.

If you're using Masonry to set up the constraints within a cell's contentView, you must be very careful to avoid doing things like this:

[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
...
}];

Because Masonry will automatically set translatesAutoresizingMaskIntoConstraintd to NO:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
...
}

UIKit will kindly warn you about the issue:

[LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of the contentView of a UITableViewCell is not supported and will result in undefined behavior, as this property is managed by the owning UITableViewCell. Cell: <UITableViewCell: 0x7f8e8a011400; frame = (0 0; 320 44); layer = <CALayer: 0x60800002c620>>

And of course your layout will break in various mysterious ways.

tzahola avatar Nov 14 '17 15:11 tzahola

What should I do to solve this problem, if I need to add constraints to cell.contentView? Thank you!

HDClear avatar Dec 19 '17 14:12 HDClear

Just add them via [NSLayoutConstraint activateConstraints:@[ ... ]]. Don't use Masonry for that.

tzahola avatar Dec 19 '17 14:12 tzahola

https://www.jianshu.com/p/ec9db5ab3785

kenan0620 avatar Aug 06 '19 02:08 kenan0620