Collapse SwiftUI right sidebar with toolbar button

Viewed 944

My macOS app has a NavigationView with 3 columns where the left and right-most ones act as sidebars that can be collapsed. I managed to get the left sidebar to collapse by placing a toolbar button with NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil), but as far as I know it only works for the first column of a Split View.

How can I replicate this behaviour to my right sidebar?

My view:

NavigationView {
    switch loginInfo.loginStatus {
        LoginView()
    }
            
    #if os(macOS)
    if loginInfo.loginStatus != .notLoggedIn {
        VStack {
            CurrentlyTrackingView(loginInfo: loginInfo, user: user, presentsEditScreen: $showingEntryEditScreen).fixedSize()
            TimeEntriesView()
        }
                
        VStack {
            CurrentEntryView(loginInfo: loginInfo, user: user, entry: entrySidebar.entry, withName: entrySidebar.startName, bulkEdit: entrySidebar.bulkEdit, withStartDate: entrySidebar.startDate, withEndDate: entrySidebar.endDate)
            Spacer()
        }
    }
    #endif
}
.toolbar {
    ToolbarItem(placement: .navigation) {
        #if os(macOS)
        Button(action: toggleLeftSidebar, label: {
            Image(systemName: "sidebar.left")
        })
        #endif
    }
}

func toggleLeftSidebar() {
    #if os(macOS)
    NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
    #endif
}
1 Answers

it's not possible to implement with SwiftUI yet as shown in Apple Forums

Related