Skip to content

Instantly share code, notes, and snippets.

@uchcode
Created January 28, 2020 16:20
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 uchcode/1ab69f287e196053a1464c4ea4c69c1a to your computer and use it in GitHub Desktop.
Save uchcode/1ab69f287e196053a1464c4ea4c69c1a to your computer and use it in GitHub Desktop.
SwiftUI NavigationView example for iPad
import SwiftUI
var hostingController: UIViewController?
func getUISplitViewController() -> UISplitViewController {
if hostingController?.children.count == 0 {
fatalError()
}
guard let split = hostingController?.children[0] as? UISplitViewController else {
fatalError()
}
return split
}
func isCollapsed() -> Bool {
getUISplitViewController().isCollapsed
}
func isAllVisible() -> Bool {
getUISplitViewController().displayMode == .allVisible
}
func showPrimary() {
let split = getUISplitViewController()
if split.isCollapsed {
return
}
UIView.animate(withDuration: 0.3, animations: {
split.preferredDisplayMode = .primaryOverlay
}) { _ in
split.preferredDisplayMode = .automatic
}
}
func hidePrimary() {
let split = getUISplitViewController()
if split.isCollapsed || split.displayMode == .allVisible {
return
}
DispatchQueue.main.async {
split.preferredDisplayMode = .primaryHidden
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
split.preferredDisplayMode = .automatic
}
}
// =====
struct DetailView: View {
var item: Int
var body: some View {
VStack {
Text("Item \(self.item)")
if !isCollapsed() && !isAllVisible() {
Button(action: showPrimary) {
Image(systemName: "sidebar.left")
}
}
}
.onAppear {
hidePrimary()
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(1...100, id: \.self) { i in
NavigationLink(destination: DetailView(item: i)) {
Text("Item \(i)")
}
}
}
VStack {
Button(action: showPrimary) {
Image(systemName: "sidebar.left")
}
Text("or swipe")
}
}
}
}
import PlaygroundSupport
hostingController = UIHostingController(rootView: ContentView())
PlaygroundPage.current.setLiveView(hostingController!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment