Cocoa如何用多个显示器记住窗口位置?

时间:2021-05-05 23:28:26

To save window position for one(main) monitor i'm using this code:

要保存一个(主)监视器的窗口位置,我正在使用此代码:

[[win windowController] setShouldCascadeWindows:NO]; 
[win setFrameAutosaveName:@"My App"];

It's works fine. But if I connect second monitor and drag my app window to second monitor, cocoa autosave does not work - after restart, window always placed on primary(main) monitor. Any ideas? Thank you!

它工作正常。但是,如果我连接第二台显示器并将我的应用程序窗口拖到第二台显示器,则cocoa自动保存不起作用 - 重新启动后,窗口始终位于主(主)监视器上。有任何想法吗?谢谢!

1 个解决方案

#1


6  

Enable restore for your app:

为您的应用启用还原:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"NSQuitAlwaysKeepsWindows"];
}

Set your window to be restorable in Interface Builder

在Interface Builder中将窗口设置为可恢复

Also make sure that you don't call invalidate on close. It is just promise -> you might end app with deleted state

还要确保在关闭时不要调用invalidate。它只是承诺 - >你可能会以删除状态结束应用程序

[self.window invalidateRestorableState];

To verify/debug check if the content of "~/Library/Saved Application State/yourbundleidentifier" persists after restart.

验证/调试检查“〜/ Library / Saved Application State / yourbundleidentifier”的内容是否在重启后仍然存在。

#1


6  

Enable restore for your app:

为您的应用启用还原:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"NSQuitAlwaysKeepsWindows"];
}

Set your window to be restorable in Interface Builder

在Interface Builder中将窗口设置为可恢复

Also make sure that you don't call invalidate on close. It is just promise -> you might end app with deleted state

还要确保在关闭时不要调用invalidate。它只是承诺 - >你可能会以删除状态结束应用程序

[self.window invalidateRestorableState];

To verify/debug check if the content of "~/Library/Saved Application State/yourbundleidentifier" persists after restart.

验证/调试检查“〜/ Library / Saved Application State / yourbundleidentifier”的内容是否在重启后仍然存在。