gpt4 book ai didi

swift - 在 SwiftUI 中同时打开多个预览

转载 作者:搜寻专家 更新时间:2023-11-01 06:11:11 56 4
gpt4 key购买 nike

我知道我们可以在 SwiftUI 中同时打开不同屏幕的多个预览。任何人都可以帮助我实现这一目标的步骤吗?

最佳答案

您可以按名称提供设备以同时预览多个设备。例如,要预览“iPhone SE”和“iPhone XS Max”,您可以执行以下操作:

#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
ContentView()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
#endif

支持的设备列表:

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension View {

/// Overrides the device for a preview.
///
/// If `nil` (default), Xcode will automatically pick an appropriate device
/// based on your target.
///
/// The following values are supported:
///
/// "Mac"
/// "iPhone 7"
/// "iPhone 7 Plus"
/// "iPhone 8"
/// "iPhone 8 Plus"
/// "iPhone SE"
/// "iPhone X"
/// "iPhone Xs"
/// "iPhone Xs Max"
/// "iPhone Xʀ"
/// "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"
public func previewDevice(_ value: PreviewDevice?) -> Self.Modified<_TraitWritingModifier<PreviewDevice?>>

/// Overrides the size of the container for the preview.
///
/// Default is `.device`.
public func previewLayout(_ value: PreviewLayout) -> Self.Modified<_TraitWritingModifier<PreviewLayout>>

/// Provides a user visible name shown in the editor.
///
/// Default is `nil`.
public func previewDisplayName(_ value: String?) -> Self.Modified<_TraitWritingModifier<String?>>
}

关于swift - 在 SwiftUI 中同时打开多个预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56697032/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com