import SwiftUI
struct ContentView: View {
@State private var bo = false
var body: some View {
Toggle("开关", isOn: $bo)
.toggleStyle(CheckBoxToggleStyle(shape: .square))
}
struct CheckBoxToggleStyle: ToggleStyle{
enum CheckBoxShape: String{
case circle
case square
}
let shape : CheckBoxShape
init(shape: CheckBoxShape = .circle){
self.shape = shape
}
func makeBody(configuration: Configuration) -> some View {
let systemName:String = configuration.isOn ? "checkmark.\(shape.rawValue).fill" : shape.rawValue
Button(action: {
configuration.isOn.toggle()
}) {
configuration.label
Image(systemName: systemName)
.resizable()
.frame(width: 30, height: 30)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
- 效果如下