我什么时候应该调用 setContentAspectRatio?

发布于 2024-12-24 15:44:19 字数 853 浏览 3 评论 0原文

我想要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2024-12-31 15:44:19

使用 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文