我想在我的UICollectionViewCompositionalLayout岁时创建装饰视图.我使用以下代码:

func createCompositionalLayout() -> UICollectionViewLayout {
    let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
        let section = self.sections[sectionIndex]

        switch section.type {
        case "mediumTable":
            return self.createMediumTableSection(using: section)
        case "smallTable":
            return self.createSmallTableSection(using: section)
        case "listTable":
            return self.createListTableSection(using: section)
        default:
            return self.createFeaturedSection(using: section)
        }
    }
    
    layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)
    let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
    self.view = collectionView
    
    let config = UICollectionViewCompositionalLayoutConfiguration()
    config.interSectionSpacing = 20
    layout.configuration = config
    return layout
}

圆形背景视图

class RoundedBackgroundView: UICollectionReusableView {
    static let reuseIdentifier = "RoundedBackgroundView"

    private var insetView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .secondarySystemFill
        view.layer.cornerRadius = 15
        view.clipsToBounds = true
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(insetView)

        NSLayoutConstraint.activate([
            insetView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
            trailingAnchor.constraint(equalTo: insetView.trailingAnchor, constant: 15),
            insetView.topAnchor.constraint(equalTo: topAnchor),
            insetView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

但我看不到背景

如何修复它?

推荐答案

好的,别担心!所以,你遇到的问题是你的装饰视图(圆角的那个)没有显示出来.要解决这个问题,我们需要告诉每个部分使用该视图作为背景装饰.

现在,让我们一步一步地了解这些更改:

我们需要创建一个NSCollectionLayoutDecorationItem的实例,并传入装饰视图的elementKind.这将创建一个新的装饰项,然后我们可以将其附加到我们的部分.

然后,我们将这个新创建的装饰项设置为layoutSection的DecorationItems属性.这样,该部分知道它应该使用我们的装饰视图作为背景.

最后,确保你在RoundedBackgroundView内设置的 colored颜色 能突出你的insetView.如果它与集合视图的背景相同,您的装饰视图将直接融入其中,并且不可见!

所以,让我们把这个放到代码中.我将以你的createFeaturedSection(using:)为例:

func createCompositionalLayout() -> UICollectionViewLayout {
    let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
        let section = self.sections[sectionIndex]

        switch section.type {
        case "mediumTable":
            return self.createSection(using: section, type: "mediumTable")
        case "smallTable":
            return self.createSection(using: section, type: "smallTable")
        case "listTable":
            return self.createSection(using: section, type: "listTable")
        default:
            return self.createSection(using: section, type: "featured")
        }
    }

    layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)
    let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
    self.view = collectionView

    let config = UICollectionViewCompositionalLayoutConfiguration()
    config.interSectionSpacing = 20
    layout.configuration = config
    return layout
}

func createSection(using section: Section, type: String) -> NSCollectionLayoutSection {
    // create items, groups, etc. here based on your type
    // I'm leaving the creation of items and groups to you as this will depend on your needs

    let layoutSection = NSCollectionLayoutSection(group: layoutGroup)

    let decorationItem = NSCollectionLayoutDecorationItem.background(elementKind: RoundedBackgroundView.reuseIdentifier)
    decorationItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
    
    layoutSection.decorationItems = [decorationItem]

    return layoutSection
}

class RoundedBackgroundView: UICollectionReusableView {
    static let reuseIdentifier = "RoundedBackgroundView"

    private var insetView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .secondarySystemFill
        view.layer.cornerRadius = 15
        view.clipsToBounds = true
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(insetView)

        NSLayoutConstraint.activate([
            insetView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
            trailingAnchor.constraint(equalTo: insetView.trailingAnchor, constant: 15),
            insetView.topAnchor.constraint(equalTo: topAnchor),
            insetView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这段代码现在负责创建装饰视图并将其注册到布局.在createSection(using:type:)米中,我们为layoutSection米设定装饰项目.装饰视图被设置为RoundedBackgroundView的一个实例,并且它是使用一些间隔插图创建的.

请记住调整createSection(using:type:)函数,以便根据需要为每种类型的节处理布局项目和组的创建.

现在,您的集合视图应该将装饰视图显示为分区中的项的背景.

Ios相关问答推荐

SWIFT图表轴订购问题

JSON响应中的键有时为Int,有时为字符串

在 React Native iOS 中加载多个 JS 包

NumberFormatter 无法使用 Xcode 15.0 beta 正确识别 iOS 17 beta 中的 Locale 货币设置

如何将 -fobjc-arc-exceptions 标志正确添加到 XCode 中?

从 PHAssets 生成图像时,iOS 应用程序因内存问题而崩溃

通过 Rosetta 打开模拟器 xcode 14 以修复滚动

AppCenter 错误:/usr/bin/xcodebuild 失败,返回码:65

如何包含 Xcode 子项目标头?

SwiftUI - 使用切换switch 切换键盘类型(默认/数字)?

向左或向右移动时如何水平翻转sprite?

如何在 SwiftUI 上制作可点击的map route?

Select UITableViewCell 时 UIView backgroundColor 消失

何时需要将应用程序源包含在测试目标中?

不变违规:应用程序 AwesomeProject 尚未注册使用静态 jsbundle 为 iOS 设备构建时

检测 iOS UIDevice 方向

使用 Swift 在一个 ViewController 中强制横向模式

Swift 中的日期到毫秒和回溯到日期

由于 GoogleSignIn、AdMob 提交应用程序时 iOS 10 GM 发布错误应用程序try 在没有使用说明的情况下访问隐私敏感数据

两个数字之间的动画UILabel文本?