I'm creating an app with a NavigationSplitView on iPadOS. The sidebar is, for all intents and purposes, a very standard list of navigation links. The detail column shows information on the selected item. Nothing out of the ordinary.
However, we've recently added an 'overview' screen, presented in the detail column, showing a grid of items, with controls and buttons for each. To avoid confusion, I want to disable the sidebar column whenever this overview is shown. This to avoid confusion since the sidebar has most, if not all, of the info and actions that the overview has.
Practically, I think the best way to do this is to disable the sidebar button whenever the overview screen is active. Which sounds easy, but with my limited UIKit experience I haven't been able to produce a good alternative.
This is what I have so far. It works technically, but the layout and some functionality is missing compared to the NavigationSplitView; specifically the navigation bar is showing some very weird layout bugs (toolbar buttons get redrawn on every view update, for example).
struct CustomNavigationSplitView<S: View, D: View>: View {
@Binding var enableSidebarButton: Bool
@ViewBuilder var sidebar: () -> S
@ViewBuilder var detail: () -> D
var body: some View {
CustomNavigationSplitViewController(enableSidebarButton: $enableSidebarButton,
sidebar: sidebar(),
detail: detail())
}
}
struct CustomNavigationSplitViewController<S: View, D: View>: UIViewControllerRepresentable {
@Binding var enableSidebarButton: Bool
let sidebar: S
let detail: D
func makeUIViewController(context: Context) -> UISplitViewController {
UISplitViewController(style: .doubleColumn)
}
func updateUIViewController(_ uiViewController: UISplitViewController, context: Context) {
uiViewController.viewControllers = [
getNavigationController(from: sidebar),
getNavigationController(from: detail)
]
// Enable or disable sidebar button
// ...
}
@MainActor private func getNavigationController(from view: some View) -> UINavigationController {
let hostingController = UIHostingController(rootView: view)
return UINavigationController(rootViewController: hostingController)
}
}
Am I doing something wrong in building the custom UIViewControllerRepresentable?
Preferrably, is it possible to programmatically enable/disable the sidebar button in SwiftUI's built-in NavigationSplitView?
Note: Using the new toolbar(removing:) modifier is unfortunately not possible, as most of our clients are unable to migrate to iOS 17 as of yet.