'horizontal(layoutSize:subitem:count:)' 在 iOS 16.0 中已弃用

'horizontal(layoutSize:subitem:count:)' was deprecated in iOS 16.0

提问人:danu 提问时间:10/4/2023 更新时间:10/17/2023 访问量:190

问:

我使用 UICollectionViewCompositionalLayout 将我的集合视图项目并排放置,用于正常情绪的横向和单个磁贴。因此,我使用 NSCollectionLayoutGroups 来实现此目标。当我定义我的组时,我收到提到的错误。我的代码如下。

private func layout() -> UICollectionViewCompositionalLayout {
    
    let layout = UICollectionViewCompositionalLayout { sectionIndex, environment -> NSCollectionLayoutSection? in
        
        let itemSize = NSCollectionLayoutSize (
            widthDimension: .fractionalWidth(0.2),
            heightDimension: .estimated(400)
        )
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        let groupSize = NSCollectionLayoutSize (
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .estimated(400)
        )
        
        let columns = (environment.container.contentSize.width > 500 && self.toggleSwitch.isOn) ? 2 : 1
        
       //MARK: Error horizontal(layoutSize:subitem:count:)' was deprecated in iOS 16.0
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: columns)
        
        group.interItemSpacing = .fixed(20)
        
        if columns > 1 {
            group.contentInsets.leading = 20
            group.contentInsets.trailing = 20
        }
        
        let section = NSCollectionLayoutSection(group: group)
        section.interGroupSpacing = 20
        section.contentInsets.top = 20
        
        return section
    }
    
    let config = UICollectionViewCompositionalLayoutConfiguration()
    config.interSectionSpacing = 50
    layout.configuration = config
    
    return layout
}
iOS Swift UIColitalView UIKithCollectionViewCompositionalLayout

评论


答:

0赞 teacup 10/17/2023 #1

它已被替换为 Apple 开发者网站上的记录。以下是更新代码的方式:horizontal(layoutSize:repeatingSubitem:count:)

let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, repeatingSubitem: item, count: columns)