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

func createListTableSection(using section: Section) -> NSCollectionLayoutSection {
    
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1/3), heightDimension: .fractionalHeight(1))
    let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
    layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)
        
    let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalWidth(0.5))
        
    let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitems: [layoutItem])

    let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
        
    layoutSection.orthogonalScrollingBehavior = .groupPagingCentered
        
    layoutSection.decorationItems = [NSCollectionLayoutDecorationItem.background(elementKind: RoundedBackgroundView.reuseIdentifier)]
    let layout = UICollectionViewCompositionalLayout(section: layoutSection)
    layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)

    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")
    }
}

我得到了这个错误:

线程1:"无法将种类的装饰视图出列: 必须注册为类或nib或连接 故事板中的原型"

如何修复它?

推荐答案

这意味着该视图没有正确注册,或者名称和名称不匹配,无法将该视图出列.

createListTableSection(using:)是登记的,但那是错误的.这是为了创建NSCollectionLayoutSection,而不是UICollectionView的布局.

注册应该在你创建UICollectionViewCompositionalLayout的地方.这是您通常要做的事情:在其中初始化集合视图或在视图控制器的viewDidLoad中.

例:

// Assuming your collection view is created something like this:
let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
    // Here, you should call your createListTableSection function
    return self.createListTableSection(using: someSection)
}
layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

// Then, you set the collectionView to your controller's view
self.view = collectionView

如果您仍然收到错误,请判断您的重用标识符是否正确.

Ios相关问答推荐

使用UIGraphicsBeginIMAext创建新图像会扭曲图像 colored颜色

RN 0.63支持iOS ProMotion?

我如何确保用户继续他们在应用程序中停止的地方?

更改文本在Flutter 中的位置

SWIFT AVFoundation翻转相机功能不起作用

Xamarin Forms CustomPin iOS Render

ios PWA从外部链接返回后降低了innerheight

SwiftUI:.toolbar 不适用于 UITextView

仅更改 AttributedString 的字符串(例如 UIButton 配置 attributeTitle)

如何从单独的视图更改修饰符的值

圆角矩形路径上的蛇形动画

iOS 16 中的状态栏 colored颜色

Flutter 构建 ios 失败:运行 pod 安装时出错

如何在 SwiftUI 中将选项卡放在滚动视图中?

弹出视图后使所有背景 UI 模糊

SwiftUI Select 器值在超过最大值时重置

Xcode 8,iOS 10 -为进程启动 WebFilter 日志(log)记录

我可以通过 UIAppearance 代理设置哪些属性?

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

检测应用程序何时进入我的视图背景的最佳方法是什么?