首页 > 解决方案 > SwiftUI:UIViewRepresentable 小部件预览

问题描述

要预览UIViewRepresentable结构,我有以下代码:

import SwiftUI
import WidgetKit

struct ViewRepresentable: UIViewRepresentable {
  func makeUIView(context: Context) -> UIView {
    return UIView()
  }

  func updateUIView(_ uiView: UIView, context: Context) {

  }

  typealias UIViewType = UIView
}

struct ViewRepresentable_Previews: PreviewProvider {
  static var previews: some View {
    ViewRepresentable().previewContext(WidgetPreviewContext(family: .systemSmall))
  }
}

它输出以下诊断信息。

RemoteHumanReadableError: Failed to update preview

invalid TimelineEntries

错误是“无法在此文件中预览 - 无法更新预览”

你如何解决这个错误?

标签: iosswiftuiwidgetkit

解决方案


包裹在 UIViewRepresentable 中的 UIKit 视图在 WidgetKit 中不起作用。

来源:https ://developer.apple.com/forums/thread/653471?answerId=619627022#619627022


推荐阅读