Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Created March 19, 2020 19:24
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 josefrichter/9f0e9eb77511af81dcd15b265c23187a to your computer and use it in GitHub Desktop.
Save josefrichter/9f0e9eb77511af81dcd15b265c23187a to your computer and use it in GitHub Desktop.
//
// Modal sheets on top of each other in SwiftUI
// Testing the possibilities
//
// ContentView.swift
// navTest1
//
// Created by JOSEF RICHTER on 04/03/2020.
// Copyright © 2020 JOSEF RICHTER. Licence: do whatever you want, aka WTFPL http://www.wtfpl.net
//
//
import SwiftUI
private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
return dateFormatter
}()
struct ContentView: View {
@State private var dates = [Date]()
var body: some View {
NavigationView {
MasterView(dates: $dates)
.navigationBarTitle(Text("Master"))
.navigationBarItems(
leading: EditButton(),
trailing: Button(
action: {
withAnimation { self.dates.insert(Date(), at: 0) }
}
) {
Image(systemName: "plus")
}
)
DetailView()
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
struct MasterView: View {
@Binding var dates: [Date]
var body: some View {
List {
ForEach(dates, id: \.self) { date in
NavigationLink(
destination: DetailView(selectedDate: date)
) {
Text("\(date, formatter: dateFormatter)")
}
}.onDelete { indices in
indices.forEach { self.dates.remove(at: $0) }
}
}
}
}
struct DetailView: View {
var selectedDate: Date?
@State var isModal: Bool = false
@State var isSubModal: Bool = false
var modal: some View {
Group {
Text("Modal")
.padding(20)
Button("Show SubModal") {
self.isSubModal = true
}.sheet(isPresented: $isSubModal, content: {
self.submodal
})
}
}
var submodal: some View {
Text("SubModal")
.padding(20)
}
var body: some View {
Group {
if selectedDate != nil {
Text("\(selectedDate!, formatter: dateFormatter)")
.padding(20)
NavigationLink(destination: SubView()) {
Text("Push one level deeper")
.padding(20)
}
Button("Show Modal") {
self.isModal = true
}.sheet(isPresented: $isModal, content: {
self.modal
})
} else {
Text("Detail view content goes here")
}
}.navigationBarTitle(Text("Detail"))
}
}
struct SubView: View {
var body: some View {
Group {
Text("Subdetail view content goes here")
}.navigationBarTitle(Text("Subdetail"))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment