将uilabel/uiimageView添加到uicontentviewcell扰乱它的大小
我有一个在故事板上定义的UicollectionViewCell,它在其contentview中添加了Uilabel。收集视图使用流程布局,我在Flowlayout委托中返回一个固定的单元格大小,如下所示:
let sizeOfItem = CGFloat(210.0)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: sizeOfItem, height: sizeOfItem)
}
我在故事板中添加了Uilabel的以下约束,并且该单元格自动开始调整大小以匹配Uilabel中的文本大小。这不是我想要的。我希望该单元格是固定的,并标记为AutoShrink。
我什至尝试将标签的ContentHuggingPriority设置为最低值(即1)。如果标签中的文本很小,这会阻止单元自动缩短。但是,当文本很大时,细胞仍然会生长。我也不希望这种情况发生。我希望该单元格的尺寸固定,并由代表中的SizeForitem返回,并将标签调整为字体大小。
编辑:我还将contentCompressionProperty设置为最低,然后可以工作。但是,我想知道解决此类方案的正确方法是什么,其中contentview不取决于子视图。
编辑2:当contentView中具有UIImageView时,问题也出现,并且图像比定义的单元格大小更大或更小。设置UIImageView的内在内容大小无济于事。
I have a UICollectionViewCell defined in storyboard which has a UILabel added to it's contentView. Collection view uses a flow layout and I return a fixed size of cell in flowlayout delegate as follows:
let sizeOfItem = CGFloat(210.0)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: sizeOfItem, height: sizeOfItem)
}
I added the following constraint for UILabel in storyboard and the cell automatically starts resizing itself to match the text size in UILabel. This is not what I want. I want the cell to be fixed size and label to autoshrink instead.
I even tried setting contentHuggingPriority of label to lowest value (i.e. 1). This stops the cell from auto-shrinking if the text in label is small. But still the cell grows when the text is big. I don't want this to happen either. I want the cell to be fixed size as returned by sizeForItem in delegate and the label to adapt it's font size.
EDIT: I also set contentCompressionProperty to lowest and it then works. But I am wondering what is the right way to fix this kind of scenario where contentView does not depend on subviews.
EDIT 2: The problem also appears when having UIImageView in contentView and the image is bigger or smaller than defined cell size. Setting intrinsic content size of UIImageView does not help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了获得委托方法中指定的确切单元格大小,您不能使用相等的宽度约束,因为它将是一个动态大小的单元格。
To get the exact cell size as specified in the delegate method, you can't use an equal width constraint since then it will be a dynamic-size cell.