如何制作一个水平滚动的2行多列UICollectionView

时间:2016-05-25 22:26:09

标签: swift uicollectionview uicollectionviewlayout

我想制作一个看起来像这样的UICollectionView

[ - - - - - 0 - - - - - ] -> Scrolls Horizontally
[1][2][3][4][5][6][7][8]  -> Scrolls Horizontally

怎么做?

我不想将UICollectionView放在UITableViewCell中。

1 个答案:

答案 0 :(得分:0)

让您的子类继承CollectionViewFlowLayoutDelegate协议

func collectionView(collectionView: UICollectionView,
                      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let size = CGSize(width: defaultWidth, height: collectionView.height / 2)
    //The important thing here is the height of your item is half the height of collectionView and then your flowlayout will start to put them on top of eachother.
    // Also make sure that height accounts for section and content insets
    return size
}

然后在viewDidLoad()中确保将collectionView的滚动属性设置为仅水平滚动。