mac 设备的 SwiftUI 预览

SwiftUI Preview of mac device in preview

当我开发 macOS 应用程序时

中没有真实设备,如何将 Xcode 预览渲染到 iMac 设备
   struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
           ContentView()
    }
   }

我只看到我写的代码,但我想在我写一行代码之前看到整个设备 Apple 是如此不一致...

我 运行 Xcode 在 MacBook 和 iMac Pro 上,希望看到这两款设备的预览

我们非常重视任何帮助!

尝试以下操作:

struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
       ContentView()
           .previewDevice(PreviewDevice(rawValue: "Mac"))
   }
}

您可以为该 rawValue 指定的设备列表(截至目前)是:

  • "Mac"
  • "iPhone 7"
  • "iPhone 7 Plus"
  • "iPhone 8"
  • "iPhone 8 Plus"
  • "iPhone SE"
  • "iPhone X"
  • "iPhone Xs"
  • "iPhone Xs Max"
  • "iPhone Xr"
  • "iPad mini 4"
  • "iPad Air 2"
  • "iPad Pro (9.7-inch)"
  • "iPad Pro (12.9-inch)"
  • "iPad (5th generation)"
  • "iPad Pro (12.9-inch) (2nd generation)"
  • "iPad Pro (10.5-inch)"
  • "iPad (6th generation)"
  • "iPad Pro (11-inch)"
  • "iPad Pro (12.9-inch) (3rd generation)"
  • "iPad mini (5th generation)"
  • "iPad Air (3rd generation)"
  • "Apple TV"
  • "Apple TV 4K"
  • "Apple TV 4K (at 1080p)"
  • "Apple Watch Series 2 - 38mm"
  • "Apple Watch Series 2 - 42mm"
  • "Apple Watch Series 3 - 38mm"
  • "Apple Watch Series 3 - 42mm"
  • "Apple Watch Series 4 - 40mm"
  • "Apple Watch Series 4 - 44mm"

Apple 的文档在这方面有点乏味,但可以在 link 找到一些相关信息: https://developer.apple.com/documentation/swiftui/previewdevice

我花钱买了一个 macOS 应用程序,它在筛选 SwiftUI 文档方面做得非常出色(比 Apple 推出的更好)。它有点贵,但如果您有兴趣,可以在以下位置找到它: https://apps.apple.com/us/app/a-companion-for-swiftui/id1485436674?mt=12