[SOLVED] SwiftUI – How to disable sidebar from collapsing?

Issue

Gif to understand easier

Is there any way to disable collapsibility of SidebarListStyle NavigationViews?

Solution

Using this SwiftUI Introspection library:
https://github.com/siteline/SwiftUI-Introspect

We can introspect the underlying NSSplitView by extending their functionality:

public func introspectSplitView(customize: @escaping (NSSplitView) -> ()) -> some View {
    return introspect(selector: TargetViewSelector.ancestorOrSibling, customize: customize)
}

And then create a generic extension on View:

public extension View {
    func preventSidebarCollapse() -> some View {
        return introspectSplitView { splitView in
            (splitView.delegate as? NSSplitViewController)?.splitViewItems.first?.canCollapse = false
        }
    }
}

Which can be used on our sidebar:

var body: some View {
    (...)
    MySidebar()
        .preventSidebarCollapse()
}

Answered By – Oskar

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.