欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SwiftUI - Button

程序员文章站 2024-03-24 13:17:22
...

文章目录



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

SwiftUI - Button


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)
        }
    }
}