swiftui
My SwiftUI quick tip for this week covers custom Toggle
views! You can easily apply your own style to SwiftUI toggles by using the ToggleStyle
protocol. The best part is you don't need to worry about implementing any of the backing properties of the Toggle
. Simply toggle the isOn
property inside the Configuration
instance that's passed from the makeBody(configuration:)
function.
我本周的SwiftUI快速提示涵盖自定义Toggle
视图! 您可以使用ToggleStyle
协议轻松地将自己的样式应用于SwiftUI切换。 最好的部分是,您不必担心实现Toggle
任何支持属性。 只需在从makeBody(configuration:)
函数传递的Configuration
实例中切换isOn
属性即可。
创建一个自定义的ToggleStyle
(Create a Custom ToggleStyle
)
Start off by creating a new struct, and make sure to inherit from the ToggleStyle
Protocol. Then, implement the makeBody(configuration:)
function. This is where you'll construct your custom View
to be shown in place of the default switch.
首先创建一个新的结构,并确保从ToggleStyle
协议继承。 然后,实现makeBody(configuration:)
函数。 在这里,您将构造自定义View
,以显示在默认开关的位置。
import SwiftUI
struct MyToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {