简体   繁体   English

UICollectionViewCell有2个单元格,每个单元格的高度都为“ wrap_content”

[英]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.

相关问题 UICollectionViewCell - 当其内容改变时动画单元格高度改变 - UICollectionViewCell - animate cell height change when its content changes UICollectionViewCell 内容与其他单元格重叠 - UICollectionViewCell content overlapping other cells UICollectionViewCell的动态高度取决于内容 - Dynamic height of UICollectionViewCell depending on content UICollectionViewCell高度取决于内容大小 - UICollectionViewCell Height depending on content size 有没有一种方法可以更新单个UITableViewCell的高度,而无需重新计算每个单元格的高度? - Is there a way to update the height of a single UITableViewCell, without recalculating the height for every cell? 在Objective C中动态修改UICollectionViewCell中单元格的高度 - Dynamically modifying the height of cells in a UICollectionViewCell in Objective C UICollectionViewCell Selection每5个单元格选择/突出显示一次 - UICollectionViewCell Selection selects/highlights every 5 cells tableview单元格内部的Collectionview具有动态高度单元格和动态内容swift 4 - Collectionview inside tableview cell with dynamic height cells and dynamic content swift 4 UICollectionViewCell从自定义单元格获得不明确的高度 - UICollectionViewCell gets ambiguous height from custom cell iOS:UICollectionViewCell动画部分更改单元格内容 - iOS: UICollectionViewCell animation change cells content partially
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM