如何在OSX应用程序中还原窗口位置? [英] How to restore window position in an OSX application?
问题描述
我创建了一个情节提要,它具有窗口视图控制器作为初始视图控制器.我给窗口一个自动保存的名称preferencesWindow
.在首选项中,我选择了[x]可恢复,关闭时[x]释放.
I created a storyboard it has window view controller as initial view controller. I gave the window an autosave name preferencesWindow
. In the preferences I checked [x] Restorable and [x] Release when closed.
当我进入菜单并单击首选项"时,我将像这样加载窗口控制器:
When I go into the menu and click Preferences I load the window controller like so:
let storyboard = NSStoryboard(name: "Preferences", bundle: nil)
let windowController = storyboard.instantiateInitialController() as? NSWindowController
let window = windowController?.window
windowController!.showWindow(self)
这将显示首选项视图控制器,当我将其拖动到另一个位置并单击关闭按钮时,它将关闭.到目前为止,一切都很好.但是,当我再次从菜单加载窗口时,它显示在其原始位置,而不是我上次将窗口拖动到的位置.为什么会这样?
This will present the preferences view controller and when I drag it to another position and click the close button it will close. So far so good. However when I load the window again from the menu, it shows on it's original position instead of the position I last dragged the window to. Why is this?
答案 在xCode 7中设置代码中的自动保存名称似乎可以解决该错误.
Answer It appears to be a bug in xCode 7 setting the auto save name in code solved it.
let storyboard = NSStoryboard(name: "Preferences", bundle: nil)
let windowController = storyboard.instantiateInitialController() as? NSWindowController
let window = windowController?.window
window!.setFrameAutosaveName("preferences")
windowController!.showWindow(self)
推荐答案
这是Xcode 6中的错误,我不知道Xcode 7中是否已解决.
This is a bug in Xcode 6 and I don't know if it is fixed in Xcode 7.
在InterfaceBuilder中设置自动保存无效.要使其正常工作,只需在windowController的windowDidLoad()
中设置其名称:
Setting autosave in InterfaceBuilder has no effect. To get it to work just set its name in windowDidLoad()
of your windowController:
class MyWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
self.windowFrameAutosaveName = "position"
}
}
这篇关于如何在OSX应用程序中还原窗口位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!