美文网首页SwiftUI
SwiftUI 2.0 中的菜单组件 —— Menu

SwiftUI 2.0 中的菜单组件 —— Menu

作者: 刘铁崧 | 来源:发表于2021-02-23 11:26 被阅读0次
    • 普通菜单
    struct ContentView: View {
        @State private var bgColor = Color.white
        var body: some View {
            HStack(alignment: .top){
            
                bgColor
                Menu("菜单"){
                    Section{
                        Button(action: {
                            bgColor = Color.red
                        }, label: {
                            Text("栏目一")
                        })
                    }
                    Section{
                        Button(action: {
                            bgColor = Color.yellow
                        }, label: {
                            Text("栏目二")
                        })
                        Button(action: {
                            bgColor = Color.green
                        }, label: {
                            Text("栏目三")
                        })
                    }
                }
            }
        }
    }
    
    • 分栏菜单
    struct ContentView: View {
        @State private var bgColor = Color.white
        var body: some View {
            HStack(alignment: .top){
                bgColor
                Menu {
                    Button(action: {
                        bgColor = Color.red
                    }, label: {
                        Text("栏目一")
                    })
                    Button(action: {
                        bgColor = Color.yellow
                    }, label: {
                        Text("栏目二")
                    })
                    Button(action: {
                        bgColor = Color.green
                    }, label: {
                        Text("栏目三")
                    })
                } label: {
                    Label("菜单",systemImage:"heart").foregroundColor(.gray)
                }
            }
        }
    }
    
    • 多级嵌套菜单
    struct ContentView: View {
        @State private var bgColor = Color.white
        var body: some View {
            HStack(alignment: .top){
                bgColor
                Menu {
                    Button(action: {
                        bgColor = Color.red
                    }, label: {
                        Text("栏目一")
                    })
                    Button(action: {
                        bgColor = Color.yellow
                    }, label: {
                        Text("栏目二")
                    })
                    Button(action: {
                        bgColor = Color.green
                    }, label: {
                        Text("栏目三")
                    })
                    
                    Menu("二级菜单"){
                        Section{
                            Button(action: {
                                bgColor = Color.red
                            }, label: {
                                Text("栏目一")
                            })
                        }
                        Section{
                            Button(action: {
                                bgColor = Color.green
                            }, label: {
                                Text("栏目三")
                            })
                        }
                    }
                } label: {
                    Label("菜单",systemImage:"heart").foregroundColor(.gray)
                }
            }
        }
    }
    
    • Menu + Picker
    struct ContentView: View {
        @State private var bgColor = Color.white
        @State private var selectedIndex = 0
        var colors:[Color] = [.red,.yellow,.green]
        var colorText:[String] = ["红","黄","绿"]
        var body: some View {
            HStack(alignment: .top){
                colors[selectedIndex]
                Menu("菜单"){
                    Picker("选择颜色", selection: $selectedIndex) {
                        ForEach(0..<self.colorText.count){
                            index in
                            Text(colorText[index])
                        }
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:SwiftUI 2.0 中的菜单组件 —— Menu

        本文链接:https://www.haomeiwen.com/subject/cfskfltx.html