Custom Toggle styles in SwiftUI

Tram Ho

In swiftUI, you can completely customize the Toggle itself to match the user interface.
In this article, I will guide how to customize a Toggle view. You can easily give your own styles to Toggle SwiftUIs using the ToggleStyle protocol. Simply change the isOn property inside the Configuration instance passed from the makeBody (configuration:) function.

Create a Custom ToggleStyle

To start, I create a struct MyToggleStyle and let it implement the ToggleStyle protocol. Then, execute the makeBody (configuration:) function. This is where you will create your custom view to display instead of the default switch.

Apply custom ToggleStyle to your Toggle as follows:

Some examples

Here are a few examples where I will make a sample. You are free to customize one or more custom styles for your Toggle to suit your project requirements.

CheckmarkToggleStyle

PowerToggleStyle

ImageToggleStyle


Thank you for reading !!!

Share the news now

Source : Viblo