This is a Default Button
//
// ContentView.swift
// Buttons
//
// Created by WilliamJiamin on 2024/6/20.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Button(action: {
print("This is a button!")
}) {
Text("Default")
}
}
.padding()
}
}
#Preview {
ContentView()
}
This is a Destructive Button
Button(role: .destructive, action: {
print("This is a Destructive Button")
}) {
Text("Destructive")
}
This is a Cancel Button
Button(role: .cancel, action: {
print("This is a Cancel Button")
}) {
Text("Cancel")
}
This is a button with a plain button style
Button(action: {
print("This is a button with a plain button style")
}) {
Text("Plain")
}
.buttonStyle(.plain)
This is a button with a borderless button style
Button(action: {
print("This is a button with a borderless button style")
}) {
Text("Borderless")
}
.buttonStyle(.borderless)
This is a button with a bordered button style
Button(action: {
print("This is a button with a bordered button style")
}) {
Text("Bordered")
}
.buttonStyle(.bordered)
.controlSize(.regular)
.buttonBorderShape(.automatic)
This is a button with a bordered button style but more prominent colour
Button(action: {
print("This is a button with a bordered button style but more prominent colour")
}) {
Text("Bordered")
}
.buttonStyle(.borderedProminent)
.controlSize(.regular)
.buttonBorderShape(.automatic)
Button("This is my custom Button"){
print("This is some custom code")
}
.buttonStyle(.custom)
public struct CustomButtonStyle: ButtonStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.fontWeight(.bold)
.fontWidth(.expanded)
.foregroundColor(.red)
.frame(width: 200, height: 50)
.background(.tint, in: .rect(cornerRadius: 20, style: .circular))
.opacity(configuration.isPressed ? 0.3 : 1.0)
}
}
extension ButtonStyle where Self == CustomButtonStyle {
static var custom: CustomButtonStyle {
.init()
}
}