Starbucks Caramel Frappuccino
본문 바로가기
  • 그래 그렇게 조금씩
SwiftUI/SwiftUI(Basic)

16. [SwiftUI] Button()

by Toughie 2023. 4. 20.

Button(_ title:action:) -> String title로 간편하게 만들 수 있음

Button(action:label:) -> label에는 어떤 view든 올 수 있음 (커스터마이징 자유롭게 가능)

 

action에는 버튼이 눌렸을 때 작동할 원하는 동작을 클로저로 전달하면 되고

label에는 원하는 뷰로 버튼의 모습을 커스터마이징 할 수 있다.

 

//  Created by Toughie on 2023/04/20.
//

import SwiftUI

struct Buttons: View {

    
    @State var title: String = "This is fancy title"

    var body: some View {
        VStack(spacing: 20) {
            Text(title)
                .font(.largeTitle)
            
            //MARK: - BUTTON1
            Button("Button 1") {
                self.title = "Button 1 was pressed"
            }
            .accentColor(.pink)
            
            //MARK: - BUTTON2
            Button {
                self.title = "Button 2 was pressed"
            } label: {
                Text("Button 2")
                    .font(.headline)
                    .fontWeight(.semibold)
                    .foregroundColor(.white)
                    .padding()
                    .padding(.horizontal, 20)
                    .background(
                        Color.blue
                            .cornerRadius(10)
                            .shadow(radius: 10)
                    )
            }
            //MARK: - BUTTON3
            Button {
                self.title = "Like Button was pressed"
            } label: {
                Circle()
                    .fill(Color.white)
                    .frame(width: 75, height: 75)
                    .shadow(radius: 10)
                    .overlay(
                        Image(systemName: "hand.thumbsup.fill")
                            .font(.largeTitle)
                            .foregroundColor(Color(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)))
                    )
            }
            //MARK: - BUTTON4
            Button {
                self.title = "Last Button was pressed"
            } label: {
                Text("LastButton".uppercased())
                    .font(.body)
                    .bold()
                    .foregroundColor(.gray)
                    .padding()
                    .padding(.horizontal, 10)
                    .background(
                        Capsule()
                            .stroke(Color.gray, lineWidth: 3)
                        )
            }
        }
    }
}

struct Buttons_Previews: PreviewProvider {
    static var previews: some View {
        Buttons()
    }
}