I'm trying to conditionally show a View (not using.sheet) and using a binding boolean returns the error above in the title. If I use self.menuactivated
, no view will be presented upon the @State variable toggle. Is there a way around this?
struct ContentView: View {
@State private var menuActivated = false
var body: some View {
NavigationView {
... // code
}
if $menuActivated {
menuView()
}
}
}
I assume this was the intention (menu will be shown over NavigationView on toggle)
struct ContentView: View {
@State private var menuActivated = false
var body: some View {
ZStack {
NavigationView {
... // code
}
if menuActivated {
menuView()
}
}
}
}
Found a way around it. Embedded the NavigationView
in a VStack
and added the menuView
to the end of it.
struct ContentView: View {
@State private var menuActivated = false
var body: some View {
ZStack {
NavigationView {
... // view code
}
if menuActivated {
menuView()
}
}
}
}
in case if you are looking for condition based view
func getView() -> AnyView {
return AnyView(
//any business logic and view
)
}
and use it like
self.getView()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.