0

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.

tangobango
  • 381
  • 1
  • 4
  • 17
  • You could set it to false in code. As for the cell size calculation, how is that being calculated? If you've set up your cell correctly (all the needed constraints) in your xib/storyboard, it should be able to resize quite happily without you doing any calcs – Byron Coetsee Apr 26 '20 at 21:11
  • @ByronCoetsee Thanks. Let me clarify. I am calculating the cell size based on 6 rows and 6 columns and the size of the frame. The calculation is not based on cell contents. – tangobango Apr 26 '20 at 21:30
  • Ah, right, sorry. You could try setting some constraint inside that cell to a lower priority. I've found in the past while things are being laid out, the dimensions are all weird and it breaks the constraints. Setting something to a lower priority helps that. I can't say I'm an expert with collectionViews but doesn't flowLayout let you set the number of rows/columns etc? For example like in this question: https://stackoverflow.com/questions/19010335/specify-row-and-column-number-for-uicollectionview – Byron Coetsee Apr 26 '20 at 21:46
  • @ByronCoetsee thanks for the link. I’m calculating more or less as shown there, but I see an interesting option of using different sections for the rows or columns. I’m not sure it that will help or leave me where I am. I can draw the 6x6 grid now as I want it. I’m just getting that layout error problem. – tangobango Apr 26 '20 at 23:10

1 Answers1

0

The answers to my questions:

  1. Do I have to set translatesAutoresizingMaskIntoConstraits = false? Yes.
  2. Where do I do it? In the code for the custom CollectionViewCell.

Here's what I did.

  1. Delete the prototype cell from the storyboard and do everything in code.

  2. 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.")
    }

}
  1. In the ViewController that manages the ContainerView, register the cell in viewDidLoad()
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.

tangobango
  • 381
  • 1
  • 4
  • 17