Contents

init(_:)

Creates a toggle based on a toggle style configuration.

Declaration

nonisolated init(_ configuration: ToggleStyleConfiguration)

Parameters

  • configuration:

    The properties of the toggle, including a label and a binding to the toggle’s state.

Discussion

You can use this initializer within the makeBody(configuration:) method of a ToggleStyle to create an instance of the styled toggle. This is useful for custom toggle styles that only modify the current toggle style, as opposed to implementing a brand new style.

For example, the following style adds a red border around the toggle, but otherwise preserves the toggle’s current style:

struct RedBorderToggleStyle: ToggleStyle {
    func makeBody(configuration: Configuration) -> some View {
        Toggle(configuration)
            .padding()
            .border(.red)
    }
}