【发布时间】:2021-04-30 15:16:09
【问题描述】:
我是 tvOS 开发的新手,我想创建一个每行包含 4 个项目的网格。集合应垂直滚动。我的挑战是我不确定如何处理 Apple here 在此文档中提供的信息,因为没有示例。
在 iOS 中我们会使用类似的东西:
class GridFlowLayout: UICollectionViewFlowLayout {
// here you can define the height of each cell
let itemHeight: CGFloat = 160
override init() {
super.init()
setupLayout()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupLayout()
}
/**
Sets up the layout for the collectionView. 1pt distance between each cell and 1pt distance between each row plus use a vertical layout
*/
func setupLayout() {
minimumInteritemSpacing = 1
minimumLineSpacing = 1
scrollDirection = .vertical
}
var itemWidth: CGFloat {
return collectionView!.frame.width / 4 - 1
}
override var itemSize: CGSize {
set {
self.itemSize = CGSize(width: itemWidth, height: itemHeight)
}
get {
return CGSize(width: itemWidth, height: itemHeight)
}
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return collectionView!.contentOffset
}
}
然后将其分配给collectionView布局
collectionView.collectionViewLayout = GridFlowLayout()
我不确定要为 tvOS 做什么,我应该如何调整上述代码以使用编程代码为 tvOS 工作。
【问题讨论】:
标签: swift uicollectionview tvos