2015-03-03 61 views
0

我想要做的象下面這樣: enter image description here如何在swift中動態更改集合視圖的大小?

我已經子類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 

} 

,但得到的輸出象下面這樣: enter image description here

你能告訴我我是什麼失蹤?我對swift和收集視圖都很陌生。我從https://github.com/bryceredd/RFQuiltLayout得到參考,但沒有得到它的代碼。請幫助解決我的問題

+0

你檢查的https://github.com/chiahsien/CHTCollectionViewWaterfallLayout – 2015-03-04 05:29:38

+0

@ZaidPathan你能解釋一下自己做定製佈局的方法嗎? – 2015-03-05 05:30:35

+0

這可以通過UIScrollView更加準確地實現。嘗試使用不同的幀創建UIImageView並將其添加到滾動視圖。 – 2016-03-17 11:54:57

回答

0

設置集合視圖的高度約束,並在故事板中設置一些默認值,並在您的類中爲該約束創建一個對象。

@IBOutlet weak var collectionViewHeight: NSLayoutConstraint! 

獲取後重裝()方法集合視圖的內容高度並設置高度約束的恆定值

collection_view.reloadData() 

let height = collection_view.collectionViewLayout.collectionViewContentSize().height 
       collectionViewHeight.constant = height 
collectionViewHeight.constant = height 
相關問題