Search code examples
swiftswiftui

How to add if else statement on padding?


I want to set the padding to -15 only when showBar = false. How do I add the condition for this inside padding parameters? This is for macOS 10.15 app.

@State private var showBar = true

VStack {
    WebView          
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.padding(.top, -15)

Solution

  • Use ternary operator right inside modifier, like

    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .edgesIgnoringSafeArea(.all)
    .padding(.top, showBar ? 0 : -15)  // << here !!