【发布时间】:2020-07-07 11:32:58
【问题描述】:
在我使用自定义的 UICollectionViewFlowLayout 之前,滚动功能可以正常工作,但是当我将布局设置为 UICollectionView 时,它无法再滚动,但所有内容都显示良好,因为我的布局有效。
我想制作 PinterestLayout,所以我创建了如下图所示的布局。 I am try to use this lay out into my Instagram-style app with UICollection header and cell
我的布局代码如下:
class MyLayout : UICollectionViewFlowLayout{
fileprivate let numberOfColumns = 2
weak var delegate : MyLayoutDelegate!
fileprivate var cache = [UICollectionViewLayoutAttributes]()
fileprivate var contentHeight : CGFloat = 0
fileprivate var contentWidth : CGFloat{
guard let collectionView = collectionView else {return 0.0}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}
override var collectionViewContentSize: CGSize{
return CGSize(width: contentWidth, height: contentHeight)
}
override init() {
super.init()
self.minimumLineSpacing = 1
self.minimumInteritemSpacing = 1
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepare() {
guard let collection = collectionView else {return}
let columnWidth = contentWidth / CGFloat(numberOfColumns)
let xOffset: [CGFloat] = [0,columnWidth]
var yOffset = [CGFloat](repeating: 190, count: numberOfColumns)
var columnToPlacePhoto = 0
for item in 0 ..< collection.numberOfItems(inSection: 0){
let indexPath = IndexPath(item: item, section: 0)
let photoHeight : CGFloat = delegate.collectionView(collection, numberOfColumns: numberOfColumns, heightForPhotoAtIndexPath: indexPath)
let frame = CGRect(x: xOffset[columnToPlacePhoto], y: yOffset[columnToPlacePhoto], width: columnWidth, height: photoHeight)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = frame
self.cache.append(attributes)
yOffset[columnToPlacePhoto] = yOffset[columnToPlacePhoto] + photoHeight
columnToPlacePhoto = columnToPlacePhoto < (numberOfColumns - 1) ? (columnToPlacePhoto + 1) : 0
}
}
//
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
layoutAttributes?.forEach({ (attribute) in
// I did this to keep attribute of Collection View Header, without this step, my Header doesn't show up
if attribute.representedElementKind == UICollectionView.elementKindSectionHeader{
cache.append(attribute)
}})
return cache
}
}
我将布局分配给集合视图
class ProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, {
override func viewDidLoad() {
super.viewDidLoad()
let layout = MyLayout()
layout.delegate = self
self.collectionView.collectionViewLayout = layout
self.collectionView.isScrollEnabled = true
}
我还设置了item的数量,section的数量,header的大小等等。
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
// Set size of header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 190)
}
// Set cell
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ProfilePostCell
cell.post = posts[indexPath.item]
return cell
}
我很确定我原来的收藏视图运行良好 enter image description here
但我添加以下内容后,它无法滚动。请帮我离开这里,提前谢谢。
let layout = MyLayout()
layout.delegate = self
self.collectionView.collectionViewLayout = layout
self.collectionView.isScrollEnabled = true
【问题讨论】:
-
你能把那个代码放进去吗?在 viewWillappear() 上看看会发生什么。
-
没有任何变化。
标签: ios swift scroll uicollectionview uicollectionviewlayout