简体   繁体   English

如何快速动态更改集合视图的大小?

[英]How to dynamically change size of collection view in swift?

I want to do like below:我想做如下:在此处输入图片说明

I have subclass UICollectionViewLayout and my code is as我有 UICollectionViewLayout 的子类,我的代码是

var horizontalInset = 0.0 as CGFloat
var verticalInset = 0.0 as CGFloat
var itemHeight = 0.0 as CGFloat
var _layoutAttributes = Dictionary<String, UICollectionViewLayoutAttributes>()
var _contentSize = CGSizeZero
var arrSize:[Int] = [0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,0]
var random:Int?
var increaseRow = false

// MARK: -
// MARK: Layout

override func prepareLayout() {
    super.prepareLayout()

    _layoutAttributes = Dictionary<String, UICollectionViewLayoutAttributes>() // 1

    let path = NSIndexPath(forItem: 0, inSection: 0)
    let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withIndexPath: path)

    let headerHeight = CGFloat(self.itemHeight / 4)
    attributes.frame = CGRectMake(0, 0, self.collectionView!.frame.size.width, headerHeight)

    let headerKey = layoutKeyForHeaderAtIndexPath(path)
    _layoutAttributes[headerKey] = attributes // 2

    let numberOfSections = self.collectionView!.numberOfSections() // 3

    var yOffset:CGFloat = 0.0

    for var section = 0; section < numberOfSections; section++ {

        let numberOfItems = self.collectionView!.numberOfItemsInSection(section) // 3

        var xOffset:CGFloat?

        for var item = 0; item < numberOfItems; item++ {
            let range = UInt32(19)
            random = Int(arc4random_uniform(range))
            let indexPath = NSIndexPath(forItem: item, inSection: section)
            let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) // 4

            var itemSize = CGSizeZero
            var increaseRow = false
                itemSize = randomItemSize() // 5

            if item == 0
            {
                xOffset = self.horizontalInset
                attributes.frame = CGRectIntegral(CGRectMake(xOffset!, yOffset, itemSize.width, itemSize.height))
                let key = layoutKeyForIndexPath(indexPath)
                _layoutAttributes[key] = attributes // 7
            }else

            {

                let previousIndexPath = NSIndexPath(forItem:indexPath.item-1, inSection:indexPath.section)
                let key = layoutKeyForIndexPath(previousIndexPath)
                let previousFrame:UICollectionViewLayoutAttributes = _layoutAttributes[key]!

                if xOffset! + previousFrame.size.width + itemSize.width > self.collectionView!.frame.size.width
                {

                    yOffset += self.horizontalInset+previousFrame.size.height

                    xOffset =  horizontalInset
                    attributes.frame = CGRectIntegral(CGRectMake(xOffset!, yOffset, itemSize.width, itemSize.height))
                    print("In if")
                    print("\n\(item):\(NSStringFromCGRect(attributes.frame))")

                    let key = layoutKeyForIndexPath(indexPath)
                    _layoutAttributes[key] = attributes // 7

                }else
                {
                    xOffset = xOffset! + horizontalInset + previousFrame.size.width
                    attributes.frame = CGRectIntegral(CGRectMake(xOffset!, yOffset, itemSize.width, itemSize.height))
                    print("In else")
                    print("\n\(item):\(NSStringFromCGRect(attributes.frame))")
                    let key = layoutKeyForIndexPath(indexPath)
                    _layoutAttributes[key] = attributes // 7
                }

            }

        }
    }

    yOffset += self.itemHeight // 10

    _contentSize = CGSizeMake(self.collectionView!.frame.size.width, yOffset + self.verticalInset) // 11

}

but getting output like below:但得到如下输出:在此处输入图片说明

can you tell me what I am missing?你能告诉我我错过了什么吗? I am new to swift and also with collection view.我是 swift 的新手,也有收藏视图。 And I get reference from https://github.com/bryceredd/RFQuiltLayout but not getting its code.我从https://github.com/bryceredd/RFQuiltLayout得到了参考,但没有得到它的代码。 please help to solve my question请帮助解决我的问题

Set height constraint of collection view and set some default value in storyboard and create an object for that constraint in your class.设置集合视图的高度约束并在故事板中设置一些默认值,并在您的类中为该约束创建一个对象。

@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

Get content height of collection view after reload() method and set the constant value of height constraint reload()方法后获取collection view的内容高度并设置高度约束的常量值

collection_view.reloadData()

let height = collection_view.collectionViewLayout.collectionViewContentSize().height
collectionViewHeight.constant = height

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

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