Collectionviews move weird when setting constraint

2019-08-18 10:42发布

I got three collectionViews in my application. Now I finished it as far as I wanted it to complete. The only problem which I have for several days now are the constraints. I don't know how to explain it in words so I added two pictures of the problem to the question I hope it's understandable.

What I currently have:

What I want:

2条回答
男人必须洒脱
2楼-- · 2019-08-18 10:50

Use UICollectionViewDelegateFlowLayout delegate method to archive this.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

in this method, you can programmatically set size of cells. Give sizes according to your need of each collectionView.

查看更多
神经病院院长
3楼-- · 2019-08-18 10:56

Based on your screenshots, it looks like the collection view holding the column of white dots expands its width when the superview (the screen size) changes.

It also looks like you are using Flow Layout... when the view gets wider there is enough room for 2 cells on each row.

You should be able to fix that by either:

  • constraining the width of that collection view so it doesn't expand, or
  • using a custom Collection View Layout

I'd suggest trying a width constraint first - see if you can get the layout to look the way you want.

As a side note... using collection views may not be the best approach for what you're trying to do. UIStackView might be a better option (although, I don't know what else the interface will be doing, so maybe not).

查看更多
登录 后发表回答