Canvas 正在尝试将SwiftUI预览添加到UIKit
我正在尝试将SwiftUI预览画布添加到我的uikit项目中,如下所示。但我收到一个错误。如何消除此错误以便查看预览 错误是无法在预览中将“DonationCell”类型的返回表达式转换为返回类型“UIViewController” 提前谢谢Canvas 正在尝试将SwiftUI预览添加到UIKit,canvas,swiftui,preview,Canvas,Swiftui,Preview,我正在尝试将SwiftUI预览画布添加到我的uikit项目中,如下所示。但我收到一个错误。如何消除此错误以便查看预览 错误是无法在预览中将“DonationCell”类型的返回表达式转换为返回类型“UIViewController” 提前谢谢 class DonationCell: BaseCell { private lazy var containerView: UIView = { let view = UIView() view.backgroundColor = .w
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>) {
}
}
}
您的
DonationCell
是一个UIView
,不是一个控制器,因此应该是可表示的
struct ContainerView: UIViewRepresentable {
func makeUIView(context: Context) -> DonationCell {
DonationCell()
}
func updateUIView(_ uiView: DonationCell, context: Context) {
}
}
}
它有点奏效,但没有那么快。它显示的一个单元与模拟器中的单元不同。此外,预览中也看不到任何标签或图像。模拟器运行整个应用程序,但在预览中仅使用
ContainerView()
。我看到了,但在swiftui中,它可以完美地显示一个单元格。
struct ContainerView: UIViewRepresentable {
func makeUIView(context: Context) -> DonationCell {
DonationCell()
}
func updateUIView(_ uiView: DonationCell, context: Context) {
}
}
}