JAH*_*lia 6 xcode swift swiftui xcode15 swift-macro
我找不到使用新宏在多个设备上预览视图SwiftUI的方法。它正在使用并在画布上显示并排预览,每个预览都使用正确的目标设备,但情况并非如此,这是我的代码:XCode 15#PreviewXCode 14XCode 15
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
#Preview {
ContentView()
.previewOnDevices()
}
//#Preview {
// ContentView()
// .previewOnDevices()
//}
extension View {
func previewOnDevices() -> some View {
ForEach(DeviceName.all, id: \.self) { devicesName in
previewDevice(PreviewDevice(rawValue: devicesName))
.previewDisplayName(devicesName)
}
}
}
enum DeviceName: String, CaseIterable {
//xcrun simctl list devicetypes
case iPhoneSE = "iPhone SE (3rd generation)"
case iPhone13Mini = "iPhone 13 mini"
static var all: [String] {
return DeviceName.allCases.map { $0.rawValue }
}
}
Run Code Online (Sandbox Code Playgroud)
结果是重复相同的视图:
我知道我可以从画布中的设备列表中选择我想要的任何设备,但这一次显示一个设备,这意味着我每次需要时都需要选择所需的设备,这很令人头痛,即使我有多个 #Previews,在预览XCode中将所选设备粘贴在画布中#Preview。
任何对此的想法将不胜感激。
我尝试了很多事情并做了广泛的研究。这是我的最终结论:从 Xcode 15.1 开始,无法使用#Preview宏来完成此操作。您可以在这里找到类似的答案: https: //stackoverflow.com/a/77386048/1823922。
在我看来,这种限制可能是出于性能原因而存在。目前,我已经使用旧PreviewProvider方法在三个设备上设置了预览:
因此,我注意到在预览和 SwiftUI 视图之间切换时加载时间要长得多。
无论如何,这是可以做到的,但只能这样:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewOnDevices()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
705 次 |
| 最近记录: |