开发者

Locking the Aspect Ratio of an NSDocument's window

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开发者_如何学Python 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜