zoukankan      html  css  js  c++  java
  • SwiftUI 中通过Toggle实现单选框和复选框效果

    效果如下

    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
            }
            //configuration中包含isOn和label
            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( 30, height: 30)
                }
                
    
            }
        }
    }
    
    
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
  • 相关阅读:
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
    牛客网每日一练
  • 原文地址:https://www.cnblogs.com/chaostudy/p/15034076.html
Copyright © 2011-2022 走看看