본문 바로가기
iOS/SwiftUI

Toggle

by 소토리텔러 2023. 11. 20.

Toggle

 

init(isOn: Binding<Bool>, @ViewBuilder label: () -> Label)
struct ToggleContentView: View {
    
    @State private var isOn = false
    
    var body: some View {
        Toggle(isOn: $isOn) {
            Label("WiFi", systemImage: isOn ? "wifi" : "wifi.slash")
        }
        .onChange(of: isOn) { oldValue, newValue in
	    print("\(oldValue) -> \(newValue)")
            // 토글 이벤트 확인.
        }
        .font(.title)
        .frame(width: 180)
    }
}

 

A defualt toggle style is switch. You can change the style with toggleStyle(_ style:).

Button style Toggle

Toggle(isOn: $isOn) {
    Label("WiFi", systemImage: isOn ? "wifi" : "wifi.slash")
}
.toggleStyle(.button)
.font(.title)
.frame(width: 180)

 

You can change the color with .tint(_ tint:) method.

 

Toggle tint

Toggle(isOn: $isOn) {
    Label("WiFi", systemImage: isOn ? "wifi" : "wifi.slash")
}
.onChange(of: isOn) { oldValue, newValue in
    print("\(oldValue) -> \(newValue)")
}
.tint(.red)
.font(.title)
.frame(width: 180)

'iOS > SwiftUI' 카테고리의 다른 글

ProgressView  (0) 2023.11.21
TextField & SecureField  (0) 2023.11.21
Alert & ConfirmationDialog  (2) 2023.11.20
새 화면 띄우기  (0) 2023.11.20
다크모드 색상지원  (0) 2023.11.19