Tuesday, October 01, 2024

Create a custom toggle button in SwiftUI

Sample Code:

struct CustomToggleButton: View {

    @Binding var isSwitchOn: Bool

    

    var body: some View {

        Button(action: {

            isSwitchOn.toggle()

        }){

            HStack {

                Image(systemName: isSwitchOn ? "checkmark.circle.fill" : "circle")

                    .resizable()

                    .frame(width: 20, height: 20)

                //Text(isSwitchOn ? "On" : "Off")

            }

        }

    }

}

Usage:  

struct ContentView: View {

 @State private var toggleState = false

    

    var body: some View {

         CustomToggleButton(isSwitchOn: $toggleState)

    }

}

No comments:

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...