kean / ThreeColumnNavigation

A minimal example of three-column navigation for iPad and macOS using SwiftUI
127 stars 6 forks source link

[Resolved] Toggle the sidebar on iPadOS #5

Open FredericRuaudel opened 3 years ago

FredericRuaudel commented 3 years ago

Hi !

Thanks for this useful project. It seems that I've found how to update this code to have the sidebar toggle button working on iPadOS by changing the display mode like this:

        #if os(macOS)
        list.toolbar {
            Button(action: toggleSidebar) {
                Image(systemName: "sidebar.left")
            }
        }
        #else
        list.toolbar {
            Button(action: {
                UIApplication.shared.setFirstSplitViewPreferredDisplayMode(.oneBesideSecondary)
            }) {
                Image(systemName: "sidebar.left")
            }
        }
        #endif
}

And to make it animated, just update the setFirstSplitViewPreferredDisplayMode() method like this:

        if let splitViewController = splitViewController {
            UIView.animate(withDuration: 0.3, delay: 0, options: []) {
                splitViewController.preferredDisplayMode = preferredDisplayMode
            } completion: { _ in }

        } else {
            DispatchQueue.main.async {
                UIView.animate(withDuration: 0.3, delay: 0, options: []) {
                    splitViewController?.preferredDisplayMode = preferredDisplayMode
                } completion: { _ in }
            }
        }

Hope it helps anyone !

FredericRuaudel commented 3 years ago

It works for iPadOS 14 & 15 but do nothing on iOS (aka on phones)