开发者

Interface Builder: determine which element has the initial focus

I hav开发者_高级运维e a Cocoa app with a table view and a few other controls. When the app launches and the window is shown, a blue focus ring is drawn around the table view.

How can I get rid of that focus ring? I'd like nothing to have the focus when the window first shows.


The window has initialFirstResponder binding that shows which control will be active when the window becomes active. Change the initialFirstResponder or adjust tableview settings in interface builder to hide the focus ring


The best way I've found of stopping any of the controls from being the first responder when a window is first displayed is in the window controller:

Swift 3:

class YourWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        // Wait a frame before setting the first responder to be the window itself.
        // We can't just set it right now, because if the first responder is set
        // to the window now the system just interprets that as meaning that we
        // want the default behavior where it automatically selects a view to be
        // the first responder.
        DispatchQueue.main.async {
            window!.makeFirstResponder(nil)
        }
    }

}

It's messy, and sometimes when the window loads you see the focus ring starting to appear on one of the controls for one frame, but I haven't found a better way yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜