SwiftUI: implementing ZStack inside of navigationBarItems shows navigationBarItems error

73 views Asked by At

I'm trying to implement a ZStack inside of navigationBarItems to load a custom alert. Here is my implementation:

    var body: some View {
        VStack{
            List(self.itemsStore.names){ item in
                Text("hello")
            }
        }
        
        .navigationBarItems(trailing: Button(action: {
            ZStack {
                ItemsAlert(isShown: $isPresented, text: $text)
            }
            
        }, label: {
            Image(systemName: "plus")
        }))
    }

On this line I'm getting this error:

enter image description here

Any of you knows why I'm getting this warning? or if is a work around this error?

I'll really appreciate your help

1

There are 1 answers

0
Asperi On

You cannot put SwiftUI view in closure - it has not sense, view should be in the view hierarchy, button can activate states to manipulate with views, like

var body: some View {
    ZStack {
        ItemsAlert(isShown: $isPresented, text: $text)
        
        VStack{
           List(self.itemsStore.names){ item in
               Text("hello")
           }
        }
    }

    
    .navigationBarItems(trailing: Button(action: {
       self.isPresented = true    // << activate state            
    }, label: {
        Image(systemName: "plus")
    }))
}