UICollectionView cells, auto layout and collection

2019-08-07 06:25发布

I have a collection view which displays cells looking like a classic table view with image, title and subtitle.

I want to be able to switch the layout to a grid showing only the images, 3 in a row.

Mockup of the two layouts

The content of the collection view cell is layouted in a storyboard with auto layout. The imageView has the following constraints:

  • Leading 0 to the left cell edge
  • Top 0 to the top cell edge
  • Bottom 0 to the bottom cell edge
  • Fixed width
  • Horizontal space to the labels

After the layout change the imageView should have constraints of zero to all edges of the container, so that it fills it completely.

At the moment when I change the layout the app crashes with Unable to simultaneously satisfy constraints.. What is the best way to fix the constraints when the layout is changed, maybe even replace the cell class?

1条回答
做自己的国王
2楼-- · 2019-08-07 07:05

Turns out the problem was in some other part of the code. You can change the collectionView layout with setCollectionViewLayout:animated:completion: and reload the visible cells in the completion block.

查看更多
登录 后发表回答