Skip to content

Instantly share code, notes, and snippets.

@nixzhu
Last active February 19, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixzhu/92a2eea9a2bb44233f3137960659474f to your computer and use it in GitHub Desktop.
Save nixzhu/92a2eea9a2bb44233f3137960659474f to your computer and use it in GitHub Desktop.
import PlaygroundSupport
import SwiftUI
struct DetailView: View {
@Environment(\.isPresented) var isPresented: Binding<Bool>?
@State var count = 0
var body: some View {
VStack(spacing: 20) {
Text("Count: \(count)")
Button(action: add) {
Text("Add")
}
if isPresented != nil {
Button(action: dismiss) {
Text("Dismiss")
}
}
}
}
func add() {
count += 1
}
func dismiss() {
isPresented?.value = false
}
}
struct ContentView: View {
var body: some View {
NavigationView {
VStack(spacing: 20) {
NavigationLink(destination: DetailView()) {
Text("Push")
}
PresentationLink(destination: DetailView()) {
Text("Present")
}
}
}
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
@Laszlo75
Copy link

Laszlo75 commented Jul 8, 2019

It works nicely, thank you.

Interestingly, if I put the PresentationLink in .navigationBarItems, it brings up the DetailView just once, after Dismiss, the link doesn't work any more. Any idea why?

@nixzhu
Copy link
Author

nixzhu commented Jul 9, 2019

@Laszlo75 I have no idea.

@maundytime
Copy link

I found it's a beta bug when I learn https://designcode.io/swiftui-modal-presentation

Second Tap Issue
In the beta, when clicked a second time, it won't present the modal again.

@famictech2000
Copy link

Does this code still work!?

getting issue with:

@Environment(.isPresented) var isPresented: Binding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment