UICollectionViewCell not forcing Cell Size

久未见 提交于 2020-06-23 15:35:11

问题


I am using

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 400, height: 600)
}

to specify my CollectionView's cell sizes. It works fine but as soon as I add a width constraint (UILabel.width == ContentView.width) to my UILabel inside the ContentView of the cell, the cell's width shrinks to the intrinsic width of the UILabel.

I am using UICollectionViewDelegateFlowLayout and horizontal scrolling.

How can I force the ContentView to stick to the cell size I specified and let the subviews follow the auto layout constraints?


回答1:


The problem is that in the storyboard your collection view's Estimate Size is configured to Automatic:

You need to set it to None if you want your sizeForItemAt implementation to be obeyed rather than the internal constraints.



来源:https://stackoverflow.com/questions/58452972/uicollectionviewcell-not-forcing-cell-size

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