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()
        }
    }
    
  • 相关阅读:
    [luogu5665]划分
    [luogu5666]树的重心
    [bzoj1854]游戏
    [bzoj1853]幸运数字
    [bzoj2245]工作安排
    [bzoj1426]收集邮票
    [bzoj2396]神奇的矩阵
    [bzoj1858]序列操作
    [bzoj1863]皇帝的烦恼
    [bzoj1432]Function
  • 原文地址:https://www.cnblogs.com/chaostudy/p/15034076.html
Copyright © 2011-2022 走看看