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

后端 未结 3 911
说谎
说谎 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:17

    Ok here are new files,

    Appdelegate

    import Cocoa
    
    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
    
    @IBAction func newWindowForTab(_ sender: Any?){
    } // without this the + button doesn't show from start
    
    }
    

    ViewController

    import Cocoa
    
    class ViewController: NSViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    }
    
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    }
    

    and WindowController

    import Cocoa
    
    class WindowController: NSWindowController {
    
    var subview: WindowController?
    
    override func windowDidLoad() {
        super.windowDidLoad()
        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }
    
    @IBAction override func newWindowForTab(_ sender: Any?) {
    
        let story = self.storyboard
        let windowVC: WindowController = story?.instantiateInitialController() as! WindowController
    
        self.window?.addTabbedWindow(windowVC.window!, ordered: .above)
        self.subview = windowVC
    
        windowVC.window?.orderFront(self.window)
        windowVC.window?.makeKey()
    }
    
    }
    

    you have to add menu item and connect it to FirstResponder in menu view to newWindowForTab: action, assign key, say cmd+t to work, this example as is just adds tab from + button and window menu options work, "move tab to new window" and "merge all windows". You can drag tab out and drop back , move tabs horizontally. Look like it works.

    done with Xcode Version 8.2 beta (8C30a)

提交回复
热议问题