Skip to main content

How to create custom bindings

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to create custom bindings 관련

SwiftUI by Example

Back to Home

How to create custom bindings | SwiftUI by Example

How to create custom bindings

Updated for Xcode 15

When we use SwiftUI's @State property wrapper it does a huge amount of work on our behalf to allow two-way bindings for user interface controls. However, we can also create bindings by hand using the Binding type, which can be provided with custom get and set closures to run when the value is read or written.

For example, this creates a trivial binding that just acts as a passthrough for another property:

struct ContentView: View {
    @State private var username = ""

    var body: some View {
        let binding = Binding(
            get: { self.username },
            set: { self.username = $0 }
        )

        return VStack {
            TextField("Enter your name", text: binding)
        }
    }
}

Download this as an Xcode projectopen in new window

Tips

When binding to a custom Binding instance, you don't need to use the dollar sign before the binding name – you're already reading the two-way binding.

Custom bindings are useful when you want to add extra logic to a binding being read or written – you might want to perform some calculations before sending a value back, or you might want to take some extra actions when the value is changed.

For example, we could create a stack of two toggle switches where both can be off and either one can be on, but both can't be on at the same time – enabling one will always disable the other. Here's how that looks in code:

struct ContentView: View {
    @State private var firstToggle = false
    @State private var secondToggle = false

    var body: some View {
        let firstBinding = Binding(
            get: { self.firstToggle },
            set: {
                self.firstToggle = $0

                if $0 == true {
                    self.secondToggle = false
                }
            }
        )

        let secondBinding = Binding(
            get: { self.secondToggle },
            set: {
                self.secondToggle = $0

                if $0 == true {
                    self.firstToggle = false
                }
            }
        )

        return VStack {
            Toggle(isOn: firstBinding) {
                Text("First toggle")
            }

            Toggle(isOn: secondBinding) {
                Text("Second toggle")
            }
        }
    }
}

Download this as an Xcode projectopen in new window

Similar solutions…
How to create constant bindings | SwiftUI by Example

How to create constant bindings
Two-way bindings in SwiftUI | SwiftUI by Example

Two-way bindings in SwiftUI
Bindings and forms | SwiftUI by Example

Bindings and forms
How to create and compose custom views | SwiftUI by Example

How to create and compose custom views
How to create a custom transition | SwiftUI by Example

How to create a custom transition

이찬희 (MarkiiimarK)
Never Stop Learning.