Skip to content

Buttons

Nick Sarno edited this page Jan 13, 2024 · 6 revisions

.asButton

Wrap a View in a Button and add a custom ButtonStyle.

Text("Hello, world!")
     .asButton {
          // action
     }

Optionally customize the ButtonStyle...

Text("Hello, world!")
     .asButton(scale: 0.95, opacity: 0.8, brightness: 0.1, action: {
          // action
     })

Convenience configurations...

Text("Hello, world!")
     .asButton(.tap, action: {
                        
     })
                
Text("Hello, world!")
     .asButton(.press, action: {
                        
     })

Text("Hello, world!")
     .asButton(.opacity, action: {
                        
     })

AsyncButton

SwiftUI Button with async closure to handle isPerformingAction state.

AsyncButton {
     try? await Task.sleep(nanoseconds: 2_000_000_000)
} label: { isPerformingAction in
     ZStack {
          if isPerformingAction {
                ProgressView()
          }
            
          Text("Hello, world!")
               .opacity(isLoading ? 0 : 1)
     }
}

.asWebLink

Wrap a View in a Link and add a custom ButtonStyle. The link will open in Safari if the URL is valid.

Text("Hello, world!")
     .asWebLink {
          URL(string: "https://www.google.com")
     }
Clone this wiki locally