Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macOS Swift: Always show window tabbar programmatically

Can anybody tell me how I can get my NSWindow to show the TabBar (incl. the "+"-Button), even if I have only one tab?

I know there is a Menu Option called "Show Tab Bar" that will lead to the tab bar showing even if you have only one tab. How can I get this functionality programmatically?

See this example of a window showing the tab bar with only one tab open

like image 948
Tterhuelle Avatar asked Nov 19 '25 08:11

Tterhuelle


2 Answers

I guess the answer is a bit late :) but still might help somebody.

Here is a solution for SwiftUI application:

  1. To get the right instance of NSWindow you can check one of these answers:
  • How to access own window within SwiftUI view?
  • How to access NSWindow from @main App using only SwiftUI?

So in the end you will end up with something like this:

var window: NSWindow?
  1. Add .onAppear modifier with the following code to your high level view in the app:
.onAppear {
    guard let window = window else { return }
    guard let tabGroup = window.tabGroup else { return }
    guard !tabGroup.isTabBarVisible else { return }
    window.toggleTabBar(.none)
}

It will trigger the same action as Show Tab Bar from Menu -> View

like image 93
Alexey Pichukov Avatar answered Nov 20 '25 21:11

Alexey Pichukov


Building upon @alexey-pichukov post, the SwiftUI version to toggle the tabGroup on

.onAppear {
  DispatchQueue.main.async {
    NSApp.windows.forEach { window in
      if let tabGroup = window.tabGroup {
        window.toggleTabBar(.none)
      }
    }
  }
}
like image 44
McCaffers Avatar answered Nov 20 '25 22:11

McCaffers