【问题标题】:SwiftUI - apply modifier in case of version of macOS is some specificSwiftUI - 在 macOS 版本的情况下应用修饰符是一些特定的
【发布时间】:2021-12-15 11:31:43
【问题描述】:

可以运行与操作系统版本相关的代码:

if #available(macOS 11.0, *) {
    Button("test", action: {})
         //.modifier1
         //.modifier2
         //.modifier3
         //.modifier4
         //.modifier5
         //.modifier6
         //.modifier7
         //.modifier8
         .foreground(.red)
} else {
    Button("test", action: {})
         //.modifier1
         //.modifier2
         //.modifier3
         //.modifier4
         //.modifier5
         //.modifier6
         //.modifier7
         //.modifier8
}

是否可以在不重复代码的情况下对单个修饰符执行相同操作?

类似语法:

Button("test", action: {})
     //.modifier1
     //.modifier2
     //.modifier3
     //.modifier4
     //.modifier5
     //.modifier6
     //.modifier7
     //.modifier8
    .if(macOS 11.0, *) { $0.foreground(.red) }

?

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/63056850/12299030
  • @Asperi 不,我需要应用与 macOS 版本相关的任何修饰符的方法。不止一个人。
  • 在“if”语句的情况下,我有类似的“银弹”:prnt.sc/1xx7pir(使用示例在屏幕截图的底部)——所以有时“银弹是现实,但不是某种奇妙” :) 只是希望这里也存在一些银弹存在的可能性:)
  • 这可能会有所帮助stackoverflow.com/a/67937702/14733292
  • @Asperi 银弹存在)不是最好的解决方案,但仍然比原生 codestyle 更好:)

标签: swift macos swiftui


【解决方案1】:

你可以试试这样的:

struct ContentView: View {
    let macOS11Available: Bool
    
    init() {
        if #available(macOS 11, *) {
            self.macOS11Available = true
        } else {
            self.macOS11Available = false
        }
    }
    
    var body: some View {
        VStack (spacing: 77) {
            Button("test modifier", action: {})
                .foregroundColor(macOS11Available ? .red : .green)
            //.modifier1
            //.modifier2
            //.modifier3
            //.modifier4
            //.modifier5
            //.modifier6
            //.modifier7
            //.modifier8
            
        }.frame(width: 444, height: 444)
    }
}

【讨论】:

  • @Andrew:我不太喜欢使用 init 的视图!它会被初始化很多次!即使在我的回答中,您也可以将macOS_11 移出视野并将其更改为公共和全局价值,在这种情况下,您将仅阅读 macOS 版本 1 次,而在应用程序运行生命周期中阅读 1000 次。 PS:最好将计算值更改为变量以获得更好的编码。
  • @swiftPunk 我的解决方案基于您的回答,并添加了您自己的代码版本。感谢您的帮助和启发 :)
【解决方案2】:

这是您要查找的内容:

struct ContentView: View {
    
    var macOS_11: Bool {
        if #available(macOS 11.0, *) { return true }
        else { return false }
    }
    
    var body: some View {
        
        Text("Hello, World!")
            .foregroundColor(macOS_11 ? .red : nil)
        
    }
    
}

2.0.0 版:

这是更好的方法:

struct ContentView: View {
    
    var body: some View {
        
        VStack {
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_11 ? .red : nil)
            
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_11_3 ? .red : nil)
            
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_12 ? .red : nil)
            
        }
        .frame(width: 200)
        .padding()
 
    }
    
}


public class MacOsVer {
    
    let higherThan_11: Bool
    let higherThan_11_3: Bool
    let higherThan_12: Bool
    
    init() {
        
        if #available(macOS 11.0, *) { higherThan_11 = true }
        else { higherThan_11 = false }
        
        if #available(macOS 11.3, *) { higherThan_11_3 = true }
        else { higherThan_11_3 = false }
        
        if #available(macOS 12, *) { higherThan_12 = true }
        else { higherThan_12 = false }
        
    }
    
    static let shared: MacOsVer = MacOsVer()
    
}

【讨论】:

  • 我的解决方案基于您的回答,并添加了一些更改自己的代码版本。感谢您的帮助和启发 :) 但您的答案仍然是您的 :)
【解决方案3】:

基于 swiftPunk

思想的解决方案
public class MacOsVer {
    static var higherThan_11: Bool {
        if #available(macOS 11.0, *) { return true }
        else { return false }
    }
    
    static var higherThan_11_3: Bool {
        if #available(macOS 11.3, *) { return true }
        else { return false }
    }
    
    static var higherThan_12: Bool {
        if #available(macOS 12, *) { return true }
        else { return false }
    }
}

所以使用 if 修饰符 (https://prnt.sc/1xx7pir)

我可以这样做:

Text("some Text")
    .bacground(.blue)
    .if(MacOsVer.higherThan_11) { $0.foregroundColor(.Red) }

【讨论】:

  • 我在这里用你的答案更新了我的答案,因为我们不需要每次都计算一个版本的可用性值,我们可以节省 cpu 资源!
  • @swiftPunk 这是问题下所有答案的组合,呵呵)
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 2023-02-11
  • 1970-01-01
  • 2012-09-10
  • 2021-04-11
  • 2021-10-23
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多