简体   繁体   English

UICollectionView 全高,允许自动布局动态宽度?

[英]UICollectionView with full height, allow autolayout dynamic width?

I need to create UICollectionView cell have full height but the dynamic width to fit a UIImageView's image.我需要创建 UICollectionView 单元格具有全高但动态宽度以适合 UIImageView 的图像。 The UIImageView's constraints equal to cell ( contentView ). UIImageView 的约束等于单元格( contentView )。 I have read about the dynamic height in this so my question is: "Is the AutoLayout allow autolayout dynamic width?".我已经阅读了关于动态高度的信息,所以我的问题是:“自动布局是否允许自动布局动态宽度?”。

To my knowledge UICollectionView doesn't handle width automatically.据我所知 UICollectionView 不会自动处理宽度。 However you can easily set the width via the delegate.但是,您可以通过委托轻松设置宽度。 I refer you to this post: Dynamic cell width of UICollectionView depending on label width我推荐你看这篇文章: UICollectionView 的动态单元宽度取决于 label 宽度

It was UILabels instead of UIImageViews but the method should be similar using UIImage size property instead of sizeWithAttributes of NSString.它是 UILabels 而不是 UIImageViews,但使用 UIImage 大小属性而不是 NSString 的 sizeWithAttributes 的方法应该类似。

EDIT: After some tests it turns out UICollectionView does handle cell width automatically the same way as automatic height.编辑:经过一些测试,事实证明 UICollectionView 确实以与自动高度相同的方式自动处理单元格宽度。 So the delegate is not necessary.所以委托不是必需的。 (see this tutorial for detailed info) (有关详细信息,请参阅本教程

However, if the images are not available immediately (remotely fetched for instance) then you should wait until all images are downloaded and then display the collection.但是,如果图像不能立即可用(例如远程获取),那么您应该等到所有图像都下载完毕后再显示集合。 Otherwise you will need to invalidate the layout after each image is displayed which can lead to visual glitches.否则,您将需要在显示每个图像后使布局无效,这可能会导致视觉故障。

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

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