Initializer

init(_:)

Creates a toggle based on a toggle style configuration.

Declaration 宣言

init(_ configuration: ToggleStyleConfiguration)
Available when Label is ToggleStyleConfiguration.Label.

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)
    }
}

See Also 参照

Creating a Toggle from a Configuration