锁定 NSDocument 窗口的长宽比
我有一个基于文档的应用程序,我试图锁定文档窗口的纵横比。文档的内容是自定义视图,我可以轻松设置宽高比。但是,当我调整窗口大小时,视图和窗口之间的空间会填充黑色背景。我尝试过在文档类中覆盖窗口的纵横比,但这似乎并不能解决问题。
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将窗口背景设置为不同的颜色吗?
在您的
NSDocument
子类中?如果覆盖-aspectRatio
有效,它应该位于NSWindow
的子类中。但是,您应该使用-setAspectRatio:
。无论窗口属于基于文档还是基于非文档的应用程序,这都有效。
您可以拥有一个实现
–windowShouldZoom:toFrame:
的窗口委托,它是NSWindowDelegate< 的一部分/代码> 协议。
由于OP正在开发一个基于文档的应用程序,没有
NSWindow
或NSWindowController
的自定义子类,因此实现:在
NSDocument
子类中是唯一的变化需要根据 Xcode 中基于文档的应用程序模板设置项目的宽高比。一般来说,我建议不要使用一个负责窗口和文档的
NSDocument
子类。在我看来,更好的设计是让NSDocument
子类仅处理文档,并创建仅处理窗口的NSWindowController
子类 - 想想 功能内聚 或 单一职责原则。Can’t you just set the window background to a different colour?
In your subclass of
NSDocument
? If overriding-aspectRatio
works, it should be in a subclass ofNSWindow
. However, you should use-setAspectRatio:
.This works regardless of the window belonging to a document- or non-document-based application.
You can have a window delegate that implements
–windowShouldZoom:toFrame:
, which is part of theNSWindowDelegate
protocol.Since the OP is developing a document-based application with no custom subclass of
NSWindow
orNSWindowController
, implementing: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 theNSDocument
subclass deal with documents only, and create a subclass ofNSWindowController
that deals with windows only — think functional cohesion or the single responsibility principle.