UIcollectionView 自定义布局及切换
layout3 = UICollectionViewFlowLayout()
layout3.itemSize = CGSize(width: (self.view.bounds.size.width/2-20), height: 200)
// layout3... 或者通过代理 UICollectionViewDelegateFlowLayout 方法去实现
重写 layoutAttributesForElements
和 layoutAttributesForItem
设置 frame
重写 prepare
设置 frame
if tag == 1{
collectView.setCollectionViewLayout(layout1, animated: true)
}else if tag == 2{
collectView.setCollectionViewLayout(layout2, animated: true)
}else {
collectView.setCollectionViewLayout(layout3, animated: true)
}
- Swift 4
- iOS 10+
- Xcode 9+
鸣谢