In my app I have a main window where I play some video using AVPlayer. In order to force a particular aspect ratio I'm using setAspectRatio: method. I placed it in my app's windowControllerDidLoadNib: so the code looks like:
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
[super windowControllerDidLoadNib:windowController];
// adjust the player aspect ratio
[windowController.window setAspectRatio:NSMakeSize(1.85,1)];
...
...
}
The problem is that this aspect ratio is not getting set until I resize the window.
Does anyone know what the problem might be here?
Any help is highly appreciated.
解决方案
The aspectRatio of a window is a constraint* upon resizing of the window. So, yeah, it doesn't have any effect until the user tries to do that.
You need to explicitly set the window's frame to a rectangle whose size has that aspect ratio, and you will need to compute that rectangle yourself.
Make sure you properly distinguish between the window's frame rectangle, the window's content rectangle (which has the same size as the window's content view), and the size of the content you intend to play. For example, you probably want the title bar (which is part of the frame) to be in addition to the “1” in that ratio, not part of it.
*Not in the NSLayoutConstraint sense, just the general sense.