繁体   English   中英

尝试将 SwiftUI 预览添加到 UIKit

[英]Trying to add SwiftUI Preview to UIKit

我正在尝试将 SwiftUI Preview canvas 添加到我的 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")
    }
}

您的DonationCellUIView ,而不是 controller,因此应具有代表性

 struct ContainerView: UIViewRepresentable {

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM