macOS, iOS

[SwiftUI] ButtonStyle

SweetDev 2021. 2. 23. 21:09

 

UIKit에서는 UIButton을 상속받은 클래스를 만들었듯...

SwiftUI에서는 ButtonStyle을 사용하게 된다!

 

 

struct HomeScrollButtonStyle: ButtonStyle {
  var isSelected = false
  func makeBody(configuration: Self.Configuration) -> some View {
    VStack {
      configuration.label
        .foregroundColor(isSelected ? Color.red : Color.black)
        .font(isSelected ? .largeTitle : .body)
      if isSelected {
        Circle().frame(width: 5, height: 5, alignment: .center)
      }
    }
  }
}

이런식으로 써주면 된다.