尝试将 SwiftUI Preview 添加到 UIKit

Trying to add SwiftUI Preview to UIKit

我正在尝试将 SwiftUI Preview canvas 添加到我的 uikit 项目中,如下所示。但是我收到一个错误。我怎样才能摆脱这个错误,以便我可以看到我的预览?

错误是无法在预览中将 'DonationCell' 类型的 return 表达式转换为 return 类型 'UIViewController'。

提前致谢。

class DonationCell: BaseCell {

private lazy var containerView: UIView = {
    let view = UIView()
    view.backgroundColor = .white
    view.layer.cornerRadius = 8
    //view.layer.applyButtonShadow()
    //view.clipsToBounds = true
    return view
}()

extension DonationCell: SetupCodeView {
func setupAdditionalConfiguration() {
    self.backgroundColor = Constants.Colors.backgroundColor
}

func buildViewHierarchy() {
    self.contentView.addSubviews(containerView)
}

func setupConstraints() {
    containerView.anchor(
        top: self.topAnchor,
        leading: self.leadingAnchor,
        bottom: self.bottomAnchor,
        trailing: self.trailingAnchor,
        padding: .init(top: 0, left: 0, bottom: 0, right: 0)
        )
       }


 import SwiftUI
struct MainPreview: PreviewProvider {
    static var previews: some View {
        ContainerView().edgesIgnoringSafeArea(.all)
    }
 struct ContainerView: UIViewControllerRepresentable {

        func makeUIViewController(context: UIViewControllerRepresentableContext<MainPreview.ContainerView>) -> UIViewController {
            return DonationCell() // Cannot convert return expression of type 'DonationCell' to return type 'UIViewController'
        }
        func updateUIViewController(_ uiViewController: MainPreview.ContainerView.UIViewControllerType, context: UIViewControllerRepresentableContext<MainPreview.ContainerView>) {

        }
    }
}

还有我的BaseCell

    class BaseCell: UICollectionViewCell {
    let font = Constants.Fonts.self
    let colors = Constants.Colors.self
    let buttonTitle = Constants.ButtonTitles.self
    let constraint = Constants.Constraints.self
    let shadow = Constants.Shadows.self
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    func setupViews() {
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not veen implemented")
    }
}

class BaseHeader: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    func setupViews() {
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not veen implemented")
    }
}

您的 DonationCell 是一个 UIView,而不是一个控制器,因此可以表示的应该是

 struct ContainerView: UIViewRepresentable {

        func makeUIView(context: Context) -> DonationCell {
            DonationCell()
        }

        func updateUIView(_ uiView: DonationCell, context: Context) {
        }
    }
}