[英]UICollectionViewCell with 2 cells, every single cell with “wrap_content” height
I am trying to have a automatic-height cells on a CollectionView. 我试图在CollectionView上有一个自动高度的单元格。
I have 2 different cells, that works. 我有2个不同的单元,可以工作。
When I rotate the device landscape
, I change to automatic height and width. 旋转设备
landscape
,将更改为自动高度和宽度。 It works. 有用。 But when getting back to
portrait
, it does not: 但是,回到
portrait
,它不会:
func checkEstimatedSize(){
if(DeviceType.IS_IPAD || UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight){
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = CGSize(width: 1, height: 1)
layout.invalidateLayout()
}
}else{
if(UIDevice.current.orientation == .portrait || UIDevice.current.orientation == .portraitUpsideDown){
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
//get width of CollectionView
layout.estimatedItemSize = CGSize(width: 0, height: 1)
layout.invalidateLayout()
}
}
}
}
When portrait, I want width same as CollectionView but height depends on the content of the cell. 纵向时,我希望宽度与CollectionView相同,但高度取决于单元格的内容。
I know there is a sizeForItemAt indexPath
, but how to check/set "autoheight" to a cell (or CollectionView). 我知道有一个
sizeForItemAt indexPath
,但是如何检查/设置单元格(或CollectionView)的“自动高度”。
Sorry, I am not an expert, that's why I am asking experts... 抱歉,我不是专家,所以才问专家...
Thank you in advance. 先感谢您。
EDIT: 编辑:
turns out that I changed layout.estimatedItemSize = CGSize(width: myCollectionView.frame.width, height: 0)
on the second else and kinda works... but now I am receiving a warning plus simulator freeze: The behavior of the UICollectionViewFlowLayout is not defined because: MyApp the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
原来我第二次更改了
layout.estimatedItemSize = CGSize(width: myCollectionView.frame.width, height: 0)
并可以工作...但是现在我收到警告和模拟器冻结: The behavior of the UICollectionViewFlowLayout is not defined because: MyApp the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
ideas? 想法?
I needed to set UICollectionViewFlowLayoutAutomaticSize
to my else: 我需要将
UICollectionViewFlowLayoutAutomaticSize
设置为else:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
checkEstimatedSize()
}
func checkEstimatedSize(){
if(DeviceType.IS_IPAD || UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight){
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = CGSize(width: 1, height: 1)
layout.invalidateLayout()
}
}else{
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
layout.invalidateLayout()
}
}
}
Like this works perfectly! 这样完美地工作!
Thank you all! 谢谢你们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.