尝试将 SwiftUI 预览添加到 UIKit

问题描述

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

错误是无法在预览中将类型为“DonationCell”的返回表达式转换为返回类型“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")
    }
}

解决方法

您的 00000200: 是一个 DonationCell,而不是控制器,因此应该具有代表性

UIView