SwiftUI - Button
程序员文章站
2024-03-24 13:17:22
...
文章目录
-
官方文档 SwiftUI Button
https://developer.apple.com/documentation/swiftui/button
struct ContentView: View
{
var body: some View
{
VStack
{
Button("First button")
{
print("---First button action.")
}
Button(action:
{
print("---Second button action.")
}) {
Text("Second button")
}
Button(action:
{
print("---Third button action.")
}) {
Image(systemName: "clock")
Text("Third button")
}
.foregroundColor(Color.white)
.background(Color.blue)
Button(action:
{
print("---padding for button.")
}){
Text("Default padding")
}
.padding(30)
.background(Color.yellow)
Button(action:
{
print("---padding for label.")
}){
Text("Default padding")
.padding(30)
.background(Color.pink)
}
Button(action:
{
print("---Button with image.")
}){
VStack
{
Image(systemName: "star")
Text("Button with image")
}
.padding()
.background(Color.yellow)
}
}
}
}
Show Sheet
struct ContentView : View
{
@State var isPresented = false
var body: some View
{
VStack
{
Button("Show modal")
{
self.isPresented = true
}.sheet(isPresented: $isPresented, content:
{
MyDetailView(message: "Model window")
})
}
}
}
struct MyDetailView: View
{
let message: String
var body: some View
{
VStack
{
Text(message)
.font(.largeTitle)
}
}
}