2017-06-20 28 views
0

我看過很多方法在UICollectionView中獲得傳送帶/一行/一節。但非工作可能,你有辦法。UICollectionViewFlowLayout一行/橫向部分

我有以下幾點:

@IBAction func openCollectionView(_ sender: Any) { 
    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.itemSize = CGSize(width: 50, height: self.view.frame.height) 
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 

    collectionView!.delegate = self 
    collectionView!.dataSource = self 
    collectionView!.register(PDCollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier") 

    collectionView?.contentOffset.x = 20 

    collectionView?.reloadData() 
    collectionView?.layoutIfNeeded() 

    collectionView!.addBlurEffect() 
    self.view.addSubview(collectionView!) 

} 
+1

這是很容易,你只需要將collectionView的佈局設置爲水平,然後在你的VC的dataSource方法中,爲section指定1,爲yourIndexArray.count指定itemsInSection –

回答

0

好 - 很簡單:

layout.itemSize = CGSize(width: (self.view.frame.size.width/2)+50, height: (self.view.frame.size.height/2)+50) 

而對於更復雜的細胞:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: 100, height: 100) 
}