How to add Tabs in a non-document-based app under macOS?

后端 未结 3 896
说谎
说谎 2021-01-12 06:44

I like to build an app in Swift 3 with Xcode 8 and it should enable the Apple tab bar. It is not document-based. I learned here, that the tabs can be enabled if I override t

3条回答
  •  耶瑟儿~
    2021-01-12 07:15

    In case above "+" will add new tab always after first window, if you close first window then it will be recreated under current window.

    there is way to get it work

    override func newWindowForTab(_ sender: Any?) {
        let wc: NSWindowController = self.storyboard?.instantiateInitialController() as! NSWindowController
        wc.window?.windowController = self
        NSApplication.shared.mainWindow!.addTabbedWindow(wc.window!, ordered: .above)
        wc.window?.orderFront(self)
    }
    

    trick in NSApplication.shared.mainWindow! you always add tab to current active window.

    and if we need create window at the end we should use this trick

    let tabbedWindows = NSApplication.shared.mainWindow!.tabbedWindows!
    let lastTabIdx = tabbedWindows.count - 1
    tabbedWindows[lastTabIdx].addTabbedWindow(wc.window!, ordered: .above)
    

提交回复
热议问题