How can I easily save the Window size and position state using Obj-C?

前端 未结 9 486
灰色年华
灰色年华 2021-02-02 09:19

What is the best way to remember the Windows position between application loads using Obj-C? I am using Interface Builder for the interface, is it possible to do this with bind

9条回答
  •  有刺的猬
    2021-02-02 09:50

    In order to restore a window, you can set the Restoration ID in Interface Builder. This will be used as part of the key under which the frame is stored in NSUserDefaults. -- but that didn't (always) work for me.

    NSWindow has setFrameUsingName(_:) etc. to configure this, like @BadmintonCat wrote, and you can serialize the window position manually, too, in case that doesn't work, either.

    The simplest solution in my app though was to use the NSWindowController.windowFrameAutosaveName property and set it to something in awakeFromNib(_:). That single line affected loading and saving successfully.

提交回复
热议问题