You shouldn't touch translatesAutoresizingMaskIntoConstraints on the contentView of UITableViewCell/UICollectionViewCells
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.
What should I do to solve this problem, if I need to add constraints to cell.contentView? Thank you!
Just add them via [NSLayoutConstraint activateConstraints:@[ ... ]]. Don't use Masonry for that.
https://www.jianshu.com/p/ec9db5ab3785