UICollectionViewCell不强制单元格大小 [英] UICollectionViewCell not forcing Cell Size

查看:166
本文介绍了UICollectionViewCell不强制单元格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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

指定我的CollectionView的像元大小.效果很好,但是只要在单元格ContentView内的UILabel中添加宽度约束(UILabel.width == ContentView.width),单元格的宽度就会缩小到UILabel的固有宽度.

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.

我正在使用UICollectionViewDelegateFlowLayout和水平滚动.

I am using UICollectionViewDelegateFlowLayout and horizontal scrolling.

如何强制ContentView坚持我指定的像元大小并使子视图遵循自动布局约束?

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

推荐答案

问题是在情节提要中,您的收藏夹视图的估计大小"配置为自动":

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

如果要遵守sizeForItemAt实施而不是内部约束,则需要将其设置为无".

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

这篇关于UICollectionViewCell不强制单元格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆