swift – UICollectionView自动调整大小和动态行数

swift – UICollectionView自动调整大小和动态行数,第1张

概述我想做这样的事情: 基本上,我使用的是UICollectionView和单元格(3个不同的.xib). 到目前为止,它的确有效. 我想做的是: >设置自动高度 >如果旋转,请向UIColectionView添加1行 2.1如果平板电脑,在肖像上将有2行和横向3行. (基本上与第2点相同,只增加1行. 我有这样的事情: extension ViewController {override func 我想做这样的事情:


基本上,我使用的是UICollectionVIEw和单元格(3个不同的.xib).

到目前为止,它的确有效.

我想做的是:

>设置自动高度
>如果旋转,请向UIColectionVIEw添加1行
2.1如果平板电脑,在肖像上将有2行和横向3行. (基本上与第2点相同,只增加1行.

我有这样的事情:

extension VIEwController {overrIDe func vIEwWillTransition(to size: CGSize,with coordinator: UIVIEwControllerTransitionCoordinator){    setSizeSize()}func setSizeSize(){    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()        }    }    myCollectionVIEw.collectionVIEwLayout.invalIDateLayout()}}

不行.此外,它冻结设备.在模拟器上工作特殊. (我相信更多的设备)

我也尝试了this,但有时它有效…

如果您需要更多信息,请告诉我.

提前谢谢大家的帮助.

解决方法 我建议你创建自己的UICollectionVIEwFlowLayout子类,它将生成所需的布局.这是一个可以使用的简单流程布局.如果我错过了什么,你可以根据自己的需要调整它.

public protocol CollectionVIEwFlowLayoutDelegate: class {    func numberOfColumns() -> Int    func height(at indexPath: IndexPath) -> CGfloat}public class CollectionVIEwFlowLayout: UICollectionVIEwFlowLayout {    private var cache: [IndexPath : UICollectionVIEwLayoutAttributes] = [:]    private var contentHeight: CGfloat = 0    private var contentWIDth: CGfloat {        guard let collectionVIEw = collectionVIEw else {            return 0        }        let insets = collectionVIEw.contentInset        return collectionVIEw.bounds.wIDth - (insets.left + insets.right)    }    public weak var flowDelegate: CollectionVIEwFlowLayoutDelegate?    public overrIDe var collectionVIEwContentSize: CGSize {        return CGSize(wIDth: self.contentWIDth,height: self.contentHeight)    }    public overrIDe func layoutAttributesForElements(in rect: CGRect) -> [UICollectionVIEwLayoutAttributes]? {        var layoutAttributesArray = [UICollectionVIEwLayoutAttributes]()        if cache.isEmpty {            self.prepare()        }        for (_,layoutAttributes) in self.cache {            if rect.intersects(layoutAttributes.frame) {                layoutAttributesArray.append(layoutAttributes)            }        }        return layoutAttributesArray    }    public overrIDe func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionVIEwLayoutAttributes? {        return self.cache[indexPath]    }    public overrIDe func prepare() {        guard let collectionVIEw = self.collectionVIEw else {            return        }        let numberOfColumns = self.flowDelegate?.numberOfColumns() ?? 1        let cellpadding: CGfloat = 8        self.contentHeight = 0        let columnWIDth = UIScreen.main.bounds.wIDth / CGfloat(numberOfColumns)        var xOffset = [CGfloat]()        for column in 0 ..< numberOfColumns {            xOffset.append(CGfloat(column) * columnWIDth)        }        var column = 0        var yOffset = [CGfloat](repeating: 0,count: numberOfColumns)        for item in 0 ..< collectionVIEw.numberOfItems(inSection: 0) {            let indexPath = IndexPath(item: item,section: 0)            let photoHeight = self.flowDelegate?.height(at: indexPath) ?? 1            let height = cellpadding * 2 + photoHeight            let frame = CGRect(x: xOffset[column],y: yOffset[column],wIDth: columnWIDth,height: height)            let insetFrame = frame.insetBy(dx: cellpadding,dy: cellpadding)            let attributes = UICollectionVIEwLayoutAttributes(forCellWith: indexPath)            attributes.frame = insetFrame            self.cache[indexPath] = attributes            self.contentHeight = max(self.contentHeight,frame.maxY)            yOffset[column] = yOffset[column] + height            column = column < (numberOfColumns - 1) ? (column + 1) : 0        }    }}

现在在你的UIVIEwController中你可以像这样使用它:

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let flowLayout = CollectionVIEwFlowLayout()    flowLayout.flowDelegate = self    self.collectionVIEw.collectionVIEwLayout = flowLayout}

在更改方向后,使collectionVIEw布局无效

overrIDe func vIEwDIDLayoutSubvIEws() {    super.vIEwDIDLayoutSubvIEws()    guard let flowLayout = self.collectionVIEw.collectionVIEwLayout as? CollectionVIEwFlowLayout else {        return    }    flowLayout.invalIDateLayout()}

现在使VIEwController符合CollectionVIEwFlowLayoutDelegate

extension VIEwController: CollectionVIEwFlowLayoutDelegate {    func height(at indexPath: IndexPath) -> CGfloat {        guard let cell = self.collectionVIEw.cellForItem(at: indexPath) else {            return 1        }        cell.layoutIfNeeded()        //get calculated cell height        return cell.systemLayoutSizefitting(UILayoutFittingCompressedSize).height    }    func numberOfColumns() -> Int {        //how much columns would be shown,depends on orIEntation        return UIDevice.current.orIEntation == .portrait ? 2 : 3    }}
总结

以上是内存溢出为你收集整理的swift – UICollectionView自动调整大小动态行数全部内容,希望文章能够帮你解决swift – UICollectionView自动调整大小和动态行数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1008444.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存