ios - 如何预览 iOS 和 WatchOS 之间共享的 SwiftUI View ?

标签 ios swiftui apple-watch preview swiftui-previews

我有一个示例 SwiftUI View ,我在带有 Apple Watch 扩展的 iOS 中使用,当我尝试在 WatchOS“设备”中预览它时出现此错误:

The run destination iPhone 12 mini is not valid for Running the scheme 'TestApp WatchKit App' I have tried everything. Restarting Xcode like suggested here was one of the first things.

最佳答案

我最终让它“工作”的方式是固定预览。
这是我的示例 View ,它位于“SharedViews”文件夹中,并且在 TestApp 和 TestApp WatchKit 扩展中有目标成员资格:

struct CustomTextView: View {
    let caption: String
    let foreground: Color
    var body: some View {
        VStack {
            Text(caption)
                .padding()
            Text("Colored\r" + caption)
                .foregroundColor(foreground)
                .padding()
        }
    }
}

//struct CustomTextView_Previews: PreviewProvider {
//    static var previews: some View {
//        CustomTextView(caption: "Shared Preview", foreground: .blue)
//    }
//}
为了在 Watch 设备中预览此 View ,我在 TestApp WatchKit 扩展中创建了一个“Helpers”文件夹,并在其中创建了一个“容器 View ”,它只是 TestApp WatchKit 扩展的成员:
struct CustomTextViewWatchPreview: View {
    var body: some View {
        CustomTextView(caption: "Watch Preview Container", foreground: .red)
    }
}

struct CustomTextViewWatchPreview_Previews: PreviewProvider {
    static var previews: some View {
        CustomTextViewWatchPreview()
    }
}
现在要预览我的 CustomTextView,我打开 ..Preview 文件并固定它的预览。然后我切换回 CustomTextView 文件,应用我的更改并在固定预览中查看它们。为了摆脱“无法预览...”错误,我像上面一样注释掉了自己的 CustomTextView 预览。
要在 iPhone 中预览,我更改了运行目标,取消注释自己的预览并取消固定观看预览。
有没有人有更好的解决方案?也许我可以同时在 iPhone 和 Apple Watch 中预览我的 View ?

关于ios - 如何预览 iOS 和 WatchOS 之间共享的 SwiftUI View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65871055/

相关文章:

objective-c - 使用带有 Storyboard的 instantiateViewControllerWithIdentifier 传递属性值

ios - 如何在 iOS 中限制用户对 Parse 的请求?

swiftui - 升级到 iOS13 beta 6/Xcode 11 beta 5 : issue "dyld: Symbol not found: _$s7SwiftUI7BindingVyxGAA0C11ConvertibleAAMc" 后

ios - 如何减少 WKInterfaceTable 中的行间距

ios - 为苹果 watch 和 iphone 应用程序创建常量?

ios - UIImage 和 UIView 的交互式模糊,如 iOS 8 spotlight

ios - 将 nib 文件添加到 ScrollView 时如何删除底部空白

swiftui - 如何将 Color 绑定(bind)到 .fill()?

swift - Swift Ui 中 ForEach 中的限制元素

ios - WKInterfaceMap Watchkit 上的动画注释