Search code examples
canvasswiftuipreview

Trying to add SwiftUI Preview to UIKit


I am trying to add SwiftUI Preview canvas to my uikit project as below. But I am receiving an error. How can I get rid off this error so I can see my preview?

Error is Cannot convert return expression of type 'DonationCell' to return type 'UIViewController' inside the preview.

Thanks in advance.

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>) {

        }
    }
}

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

Solution

  • Your DonationCell is-a UIView, not a controller, so representable should be

     struct ContainerView: UIViewRepresentable {
    
            func makeUIView(context: Context) -> DonationCell {
                DonationCell()
            }
    
            func updateUIView(_ uiView: DonationCell, context: Context) {
            }
        }
    }