锁定 NSDocument 窗口的长宽比

发布于 2024-11-12 00:12:18 字数 439 浏览 2 评论 0原文

我有一个基于文档的应用程序,我试图锁定文档窗口的纵横比。文档的内容是自定义视图,我可以轻松设置宽高比。但是,当我调整窗口大小时,视图和窗口之间的空间会填充黑色背景。我尝试过在文档类中覆盖窗口的纵横比,但这似乎并不能解决问题。

- (NSSize)aspectRatio {
    return NSMakeSize(600,480);
}

我还尝试在笔尖加载后设置窗口纵横比。当我的应用程序不是基于文档时,这就成功了。

[window setAspectRatio:window.frame.size];

我还有一个问题...假设我可以锁定窗口的宽高比,我想在用户单击“缩放”按钮以使应用程序全屏显示时覆盖它。然后,也只有那时,我才会想要填充视图和窗口之间的空间。

我已经研究了很长一段时间,但找不到解决方案。

I have a document based application and I am trying to lock the aspect ratio of the document's window. The content of the document is a custom view which I can easily set an aspect ratio. However, when I resize the window, there is a black background that fills the space between the view and the window. I have tried overriding the aspectRatio of the window in my documents class however that does not seem to do the trick.

- (NSSize)aspectRatio {
    return NSMakeSize(600,480);
}

I also tried setting the windows aspect ratio once the nib is loaded. This did the trick when my app was not document based.

[window setAspectRatio:window.frame.size];

I have one more kicker... Assuming that I can lock in the window's aspect ratio, I want to override that when the user clicks the Zoom button to put the app at full screen. Then and only then will I want to fill the space between the view and the window.

I have been researching for quite sometime and just cannot find a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

云裳 2024-11-19 00:12:18

有一个黑色背景填充视图和窗口之间的空间

您不能将窗口背景设置为不同的颜色吗?

我尝试覆盖文档类中窗口的纵横比

在您的NSDocument 子类中?如果覆盖 -aspectRatio 有效,它应该位于 NSWindow 的子类中。但是,您应该使用 -setAspectRatio:

我还尝试在笔尖加载后设置窗口宽高比。当我的应用程序不是基于文档时,这就成功了。

无论窗口属于基于文档还是基于非文档的应用程序,这都有效。

我想在用户单击“缩放”按钮以使应用程序全屏显示时覆盖该设置

您可以拥有一个实现 –windowShouldZoom:toFrame: 的窗口委托,它是 NSWindowDelegate< 的一部分/代码> 协议。


由于OP正在开发一个基于文档的应用程序,没有NSWindowNSWindowController的自定义子类,因此实现:

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    [[aController window] setAspectRatio:(NSSize){600,400}];
}

NSDocument子类中是唯一的变化需要根据 Xcode 中基于文档的应用程序模板设置项目的宽高比。

一般来说,我建议不要使用一个负责窗口和文档的 NSDocument 子类。在我看来,更好的设计是让 NSDocument 子类仅处理文档,并创建仅处理窗口的 NSWindowController 子类 - 想想 功能内聚单一职责原则

there is a black background that fills the space between the view and the window

Can’t you just set the window background to a different colour?

I have tried overriding the aspectRatio of the window in my documents class

In your subclass of NSDocument? If overriding -aspectRatio works, it should be in a subclass of NSWindow. However, you should use -setAspectRatio:.

I also tried setting the windows aspect ratio once the nib is loaded. This did the trick when my app was not document based.

This works regardless of the window belonging to a document- or non-document-based application.

I want to override that when the user clicks the Zoom button to put the app at full screen

You can have a window delegate that implements –windowShouldZoom:toFrame:, which is part of the NSWindowDelegate protocol.


Since the OP is developing a document-based application with no custom subclass of NSWindow or NSWindowController, implementing:

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    [[aController window] setAspectRatio:(NSSize){600,400}];
}

in the NSDocument subclass is the only change needed to set the aspect ratio on a project based upon the document-based application template in Xcode.

In general, I’d advise against having a NSDocument subclass that is responsible for windows as well as documents. In my opinion, a better design is to have the NSDocument subclass deal with documents only, and create a subclass of NSWindowController that deals with windows only — think functional cohesion or the single responsibility principle.

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