Collectionviews move weird when setting constraints

自闭症网瘾萝莉.ら 提交于 2019-12-13 03:55:00

问题


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:


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/53297580/collectionviews-move-weird-when-setting-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!