私の OS X アプリでは、Interface Builder を使用して、次のようなウィンドウが表示されます。
これを実現するために、右側にボタンを追加したいと思います。
これが可能である場合、どうすればそれを行うことができますか?
私の OS X アプリでは、Interface Builder を使用して、次のようなウィンドウが表示されます。
これを実現するために、右側にボタンを追加したいと思います。
これが可能である場合、どうすればそれを行うことができますか?
Interface Builder で行うことはできませんが、少しのコーディングで行うことができます。
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
NSButton *myButton = …; // Create custom button to be added to the title bar.
myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
[titleBarView addSubview:myButton]; // Add the custom button to the title bar.
Swift 2.2 およびAuto Layout、タイトル バーの右側に「OK」ボタンを作成します。
let myButton = NSButton()
myButton.title = "OK"
myButton.bezelStyle = .RoundedBezelStyle
let titleBarView = window!.standardWindowButton(.CloseButton)!.superview!
titleBarView.addSubview(myButton)
myButton.translatesAutoresizingMaskIntoConstraints = false
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))
自動レイアウトでは、ボタンのフレームをハードコードする必要はありません。また、ウィンドウのサイズを変更しても、常にタイトル バーの右側に表示されます。