我什么时候应该调用 setContentAspectRatio?
我想要一个 NSWindow,它在打开时具有 2:1 的宽高比,但在全屏时允许它具有任何比例。
我最初在 AppDelegate
中设置内容比例,如下所示:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[self window] setContentAspectRatio:NSMakeSize(2, 1)];
}
然后在窗口的 NSWindowDelegate
中设置和恢复它,如下所示:
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
NSWindow *window = [notification object];
[window setContentResizeIncrements:NSMakeSize(1, 1)];
}
- (void)windowDidExitFullScreen:(NSNotification *)notification
{
NSWindow *window = [notification object];
[window setContentAspectRatio:NSMakeSize(2, 1)];
}
我认为这可行,但我不确定 AppDelegate 是否是设置窗口大小的正确位置。我尝试按照 windowDidLoad:
的方式在窗口委托上寻找一种方法,但我看不出哪一个适用。理想情况下,我只在一处设置内容比例。
我应该把这样的每个窗口初始化代码放在哪里?
I'd like to have an NSWindow which has a 2:1 width to height ratio while it is open, but allow it to have any ratio when full screen.
I'm initially setting the content ratio in the AppDelegate
like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[self window] setContentAspectRatio:NSMakeSize(2, 1)];
}
and then setting and restoring it in the window's NSWindowDelegate
like this:
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
NSWindow *window = [notification object];
[window setContentResizeIncrements:NSMakeSize(1, 1)];
}
- (void)windowDidExitFullScreen:(NSNotification *)notification
{
NSWindow *window = [notification object];
[window setContentAspectRatio:NSMakeSize(2, 1)];
}
I think this works, but I'm not sure that the AppDelegate
is the right place to set up the window size. I've tried looking for a method on the window delegate along the lines of windowDidLoad:
, but I can't see which one would apply. Ideally I'd only set the content ratio in one place.
Where am I supposed to put per-window initialisation code like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 windowController 可能会更干净(如果您只是使用默认值,则创建一个子类)。您可以将其作为窗口的委托(并将输入/退出代码放入其中),并在 windowDidLoad 中控制窗口的初始大小:
It might be cleaner to use your windowController (create a subclass if you are just using the default). You would make that your window's delegate (and put the enter/exit code you have in that), and control the initial size of the window in windowDidLoad: