【问题标题】:How to deal with Swift UI version compatible to support older devices?如何处理兼容旧设备的 Swift UI 版本?
【发布时间】:2020-11-14 05:22:53
【问题描述】:

我正在 Xcode 12 beta WatchOS 7 上开发 Apple Watch 应用。

我也打算支持 watchOS 6。

以下代码有错误不知道如何处理,属于SwiftUI问题:

struct CompactStockListView: View {
  var body: some View {
    NavigationView {
      List(getStocks(), id: \.id) { stock in
        CompactStockRowView(stock: stock)
          .padding(.vertical, 6)
      }
      .navigationBarTitle(Text("Landmarks"))
    }
  }
}

Xcode 注意到错误:

“NavigationView”仅适用于 watchOS 7.0 或更高版本的应用程序扩展

下一步: 尝试此解决方案但未成功:

struct CompactStockListView: View {
  var body: some View {
    getListSafe() // Error
  }
  
  func getListSafe() -> View {  // Error tooo
    if #available(watchOSApplicationExtension 7.0, *) {
      return NavigationView {
        List(getStocks(), id: \.id) { stock in
          CompactStockRowView(stock: stock)
            .padding(.vertical, 6)
        }
        .navigationBarTitle(Text("Landmarks"))
      }
    } else {
      // Fallback on earlier versions
      return List(getStocks(), id: \.id) { stock in
        CompactStockRowView(stock: stock)
          .padding(.vertical, 6)
      }
    }
  }
}

【问题讨论】:

    标签: swiftui watchos watchos-6 version-compatibility


    【解决方案1】:

    试试下面的

      @ViewBuilder
      func getListSafe() -> some View {
        if #available(watchOSApplicationExtension 7.0, *) {
          NavigationView {
            List(getStocks(), id: \.id) { stock in
              CompactStockRowView(stock: stock)
                .padding(.vertical, 6)
            }
            .navigationBarTitle(Text("Landmarks"))
          }
        } else {
          // Fallback on earlier versions
          List(getStocks(), id: \.id) { stock in
            CompactStockRowView(stock: stock)
              .padding(.vertical, 6)
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多