I am working on a game for iPad that uses a 6 x 6 grid of equally sized cells, using a UICollectionView to layout and populate the cells. I have the layout working, but am getting the Unable to simultaneously satisfy constraints ... If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints
error on all of the cells.
The problem seems to be the difference between the size of the cell in the storyboard and the size of the cell that I calculate in the code. (My calculation is based on the size of the frame and the need for 6 rows and 6 columns. It is not driven by cell content.)
(To get the layout to work and for the recalculated size to be recognized, I needed to set the collectionView's estimated cell size to none. Automatic or any value results in the size of the storyboard cell being used instead of the calculated size.)
I read in many places the need to set translatesAutoresizingMaskIntoConstraints = false
if a view is created in code. Here, the view is from the storyboard but I calculate its size in code. So does that mean I need to set it to false? Where in the code would I do that? In the custom class for the cell?
Or, is the solution to the error something else?
Thanks.
The answers to my questions:
translatesAutoresizingMaskIntoConstraits = false
? Yes.Here's what I did.
Delete the prototype cell from the storyboard and do everything in code.
Set up the custom cell something like this:
class WWCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
}
required init?(coder: NSCoder) {
fatalError("Init(coder) has not been implemented.")
}
}
boardCollectionView.register(WWCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
Now my cells resize for different devices and always have a 6x6 grid, with cell sizes based on calculations made in the ViewController.