我有一个视图生成器,该视图生成器根据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))
}
}
}