Add buttons to Mac window Title Bars, system-wide

后端 未结 4 1887
难免孤独
难免孤独 2021-02-05 12:44

I want to be able to add a button to the title bar of all windows that open on a Mac.

The button will go on the right hand side, opposite the X -

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 12:54

    Swift 4 - In NSWindowController add the below code

    if let window = window {
        let myButton = NSButton()
        myButton.title = "Help"
        myButton.bezelStyle = .rounded
    
        let titleBarView = window.standardWindowButton(.closeButton)!.superview!
        titleBarView.addSubview(myButton)
        myButton.translatesAutoresizingMaskIntoConstraints = false
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))
    }
    

    Hope this is helpful.

提交回复
热议问题