Home > other >  SwiftUI: if Statement with modifiers
SwiftUI: if Statement with modifiers

Time:02-06

Is it possible to do something such as:

Vstack {
   Text("Hello, World!")
}
   if Role == "administrator" {
   .offset(y: 15)
}

Doing this right now gives an error on the var body: some View { line.

CodePudding user response:

You can do like this

struct ContentView: View {
    @State private var Role: String = "administrator"
    
    var body: some View {
        VStack {
            Text("Hello, World!")
        }
        .offset(y: Role == "administrator" ? 15 : 0)
    }
}

read more here

  •  Tags:  
  • Related