How to assign an optional Binding parameter in SwiftUI?

前端 未结 4 1086
梦毁少年i
梦毁少年i 2021-01-03 21:38

I\'m trying to build a custom NavBar with some optional Views, like a searchbar (but only if the view needs to display it).

I need to pass

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 22:24

    So below is example to use optional Binding and how to access the binding and toggle to show some view:

    struct NavBar: View {
        @Binding var showUser: Bool
        var showOptional: Binding?
        
        var body: some View {
        
        VStack {
         Button(action: { showUser.toggle() },
                label: { Text("NOT OPTIONAL") })
        
         Button(action: { showSelectBuddy?.wrappedValue.toggle()) },
                label: { Text("NOT OPTIONAL") })
        
        }
        }
        }
    
        struct UseNavbar: View {
        
        @State var showoptional = false
        @State var show = false 
        
        var body: some View {
        Text("Optional")
        .navigationBarItems(leading:
        NavBar(showUser: show), trailing: NavBar(showUser: show, showOptional: showoptional))
        }
        }
    

提交回复
热议问题