【问题标题】:Show some view, only if iOS 14, SwiftUI仅在 iOS 14、SwiftUI 时显示一些视图
【发布时间】:2020-12-17 06:59:22
【问题描述】:

我想显示一个文本视图说:

Text("Welcome to iOS 14")

如果用户使用的是 iOS 14。

如果用户使用的是其他 iOS 版本,我想显示不同的文本视图,比如:

Text("Welcome to Different iOS version") 

我怎样才能做到这一点?

【问题讨论】:

  • 什么叫TextView? TextEditor?
  • 文本视图意味着文本(“一些文本”)
  • 将开发目标设置为 iOS 13.x 并将文本编辑器带入内容视图。 Xcode 会告诉你怎么做。

标签: ios swiftui ios14


【解决方案1】:
struct ContentView: View {
    
    var systemVersion = UIDevice.current.systemVersion

    var body: some View {
        if systemVersion.starts(with: "14" ) {
            Text("welcome to ios 14")
        } else {
            Text("Welcome to Different iOS version")
        }
        
    }
}

【讨论】:

  • 我不认为这是他或她在说的。
  • 可能会,会等待。
  • 这对我有帮助,但不是我要说的。我不能投票给你,因为我没有足够的声誉,否则我会投票给你。
  • 您是否只想在 iOS 14 中显示一些视图,而在不是 iOS 14 时显示另一个视图?
  • 两个不同的视图说我要根据用户 iOS 版本显示的文本或图像。
【解决方案2】:

这也适用于版本 14.1、14.2 等...

返回一个字符串,您可以根据需要添加任意数量的房屋。您可以将这个概念应用于任何事物。

struct ContentView: View {  
  
    public var systemVersion: String? {
        return UIDevice.current.systemVersion // "14.2" in my case
    }
    
    var getMessageSystem: String {
        guard let versionOS = Double(systemVersion!)
            else { return "Not a valid iOS version" }
        switch versionOS {
            case _ where versionOS > 14: return "welcome to iOS 14"
            case _ where versionOS > 13: return "welcome to iOS 13"
            case _ where versionOS > 12: return "welcome to iOS 12"
            default: return "welcome"
        }
    }
    
    
    var body: some View {
        Text(getMessageSystem) // welcome to iOS 14
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多