Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Last active April 17, 2024 10:22
Show Gist options
  • Save alexpaul/a4db470c74d45567955e8c0b560e51dd to your computer and use it in GitHub Desktop.
Save alexpaul/a4db470c74d45567955e8c0b560e51dd to your computer and use it in GitHub Desktop.
SwiftUI dismiss modal view presentation
// Works on Xcode 11 GM as of 09/12/19
// Presenting view
// make sure the .sheet modifier is tied to the root View
import SwiftUI
struct ContentView: View {
@State private var showingModalView = false
var body: some View {
NavigationView {
Button(action: {
self.showingModalView = true
}) {
Text("Present Notes View")
}
.navigationBarTitle("Notes")
.sheet(isPresented: $showingModalView, content: {
AddNotesView()
})
}
}
}
struct ContentView_previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Presented view
import SwiftUI
struct AddNotesView: View {
@Environment(\.presentationMode) var presentationMode
@State private var note: String = ""
var body: some View {
VStack(spacing: 20) {
Spacer()
TextField("enter your note", text: $note)
.textFieldStyle(RoundedBorderTextFieldStyle.init())
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Save note")
}
Spacer()
}
}
}
struct AddNotesView_Previews: PreviewProvider {
static var previews: some View {
AddNotesView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment