In SwiftUI, is it possible to use a modifier only for a certain os target?

前端 未结 4 1685
無奈伤痛
無奈伤痛 2021-01-12 12:16

Good day! In SwiftUI, is it possible to use a modifier only for a certain os target? In the following code I would like to use the modifier .listStyle(SidebarListStyle()) on

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 13:05

    WorkingDog, I try your elegante code with a very simple code to change the text color depending on the Target... but the text stays blue on both target and does not go red on MacOS!

    import SwiftUI
    
    struct ContentView: View {
    
        var body: some View {
    
          #if os(macOS)
          return monText.foregroundColor(Color.red)
          #elseif os(iOS)
           return monText.foregroundColor(Color.blue)
          #endif
          }
    
      var monText: some View {
        Text("Hello, World!")
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

提交回复
热议问题