【问题标题】:swift tvOS vertical scroll Gridswift tvOS 垂直滚动网格
【发布时间】: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


    【解决方案1】:

    这可能有助于https://www.bignerdranch.com/blog/custom-collection-view-layouts-with-tvos-part-2/

    另外,请观看此视频:www.youtube.com/watch?v=JbQ2TOsZaNo

    在本视频中,对 UITableView 进行了解释,对 UICollectionView 也可以执行相同的步骤。

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多