简体   繁体   English

集合视图单元格宽度不正确

[英]collection view cell width is not correct

after update to ios 12 and xcode 10 the collectionView with cell sizeing cell does not work correctly anymore. 更新到ios 12和xcode 10后,具有单元格大小单元格的collectionView不再正常工作。

i have this layout in xcode 我在xcode中有此布局

在此处输入图片说明

the view is like this. 视图是这样的。

在此处输入图片说明

and this is how it looks now. 这就是现在的样子。

在此处输入图片说明

i have tried to set the collectionview view a widht constraint and modified in the 我试图将collectionview视图设置为widht约束,并在

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.widthConstraint.constant = UIScreen.main.bounds.width / 3

but nothing happens. 但什么也没发生。

You were using the wrong method to set the cells size. 您使用错误的方法来设置像元大小。 Instead of collectionView(_:cellForItemAt:) use collectionView:layout:sizeForItemAtIndexPath: 代替collectionView(_:cellForItemAt:)使用collectionView:layout:sizeForItemAtIndexPath:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let itemHeight = 60
    let itemWidth = UIScreen.main.bounds.width / 3
    return CGSize(width: itemWidth, height: itemHeight)
}

Also, you can set up items size using Interface Builder s Size Inspector , but in that case, the size of your cells will be absolute. 另外,您可以使用Interface BuilderSize Inspector设置项目大小,但是在这种情况下,单元格的大小将是绝对的。 If you want it to be 1/3 of screen width, you need to use the layout delegate. 如果希望它是屏幕宽度的1/3 ,则需要使用布局委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM