SwiftUI PreviewDevice没有设置userInterfaceIdiom —如何设置预览的userInterfaceIdiom?

时间:2020-04-25 14:52:23

标签: swiftui

我有一个视图生成器,该视图生成器根据userInterfaceIdiom运行不同的视图。我希望看到更改反映在预览中,但是仅设置预览设备不起作用。有办法使这项工作吗?

代码是这样的:

struct HomeView: View {
   @ViewBuilder
   var body: some View {
        if UIDevice.current.userInterfaceIdiom == .phone{
            PhoneHomeView()
        } else {
            PadHomeView()
        }
    }
}

以下内容无法产生预期的结果,预览始终在电话视图中。

struct HomeView_Previews: PreviewProvider {
    static let deviceNames: [String] = [
        "iPhone X",
        "iPad Pro (9.7-inch)"
    ]

    static var previews: some View {
        ForEach(deviceNames, id: \.self) { deviceName in
            HomeView()
              .previewDevice(PreviewDevice(rawValue: deviceName))
        }
    }
}

0 个答案:

没有答案