【问题标题】:Open multiple previews at the same time in SwiftUI在 SwiftUI 中同时打开多个预览
【发布时间】:2019-11-03 22:14:31
【问题描述】:

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

【问题讨论】:

    标签: swift xcode swiftui xcode11


    【解决方案1】:

    您可以按名称提供设备以同时预览多个设备。例如,要预览“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?>>
    }
    

    【讨论】:

    • 嗨,我注意到您在数组上使用 identifiable 的示例有效,并且在尝试添加 '\.Self' 作为参数值时,Apple swift Landmarks 应用教程上的代码失败。这只是在测试期间发生的 API 更改还是其他原因?
    • 现在是 ForEach(["iPhone SE", "iPhone XS Max"], id: \.self)
    • 如果不想预览整个设备,可以使用.previewLayout(PreviewLayout.fixed(width: 375, height: 80))
    【解决方案2】:

    我创建了一个GitHub Gist 来促进这一点。

    基本上创建了一个设备枚举以在某处预览(我更喜欢常量文件):

    enum MyDeviceNames: String, CaseIterable {
        case iPhoneX = "iPhone X"
        case iPhoneXs = "iPhone Xs"
        case iPhoneXsMax = "iPhone Xs Max"
    
        static var all: [String] {
            return MyDeviceNames.allCases.map { $0.rawValue }
        }
    }
    

    然后像这样使用它:

    struct SampleView_Previews_MyDevices: PreviewProvider {
        static var previews: some View {
            ForEach(MyDeviceNames.all, id: \.self) { devicesName in
                SampleView()
                    .previewDevice(PreviewDevice(rawValue: devicesName))
                    .previewDisplayName(devicesName)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2021-02-10
      • 2014-09-10
      相关资源
      最近更新 更多