SwiftUI dismiss modal sheet presented from NavigationView (Xcode Beta 5)

后端 未结 1 1358
轮回少年
轮回少年 2021-02-10 18:07

I am attempting to dismiss a modal view presented via a .sheet in SwiftUI - called by a Button which is within a NavigationViews nav

1条回答
  •  臣服心动
    2021-02-10 18:18

    You need to move the .sheet outside the Button.

    NavigationView {
      Text("test")
      .navigationBarTitle(Text("Navigation Title Text"))
      .navigationBarItems(trailing:
         Button("Add") {
           self.showModal = true
         }
      )
      .sheet(isPresented: $showModal, content: { ModalView() })
    }
    

    You can even move it outside the NavigationView closure.

    NavigationView {
      Text("test")
      .navigationBarTitle(Text("Navigation Title Text"))
      .navigationBarItems(trailing:
         Button("Add") { self.showModal = true }
      )
    }
    .sheet(isPresented: $showModal, content: { ModalView() })
    
    

    Notice you can also simplify the Button call if you have a simple text button.

    0 讨论(0)
提交回复
热议问题