Photovor
Photovor

Reputation: 403

Cocoa resize window to fit screen

I have 2 monitors, and I'd like to get my videoWindow to be placed and scaled to the size of the second monitor. I'd like to do this programmatically, as the second monitor resolution may change. I'm able to get the window placed in the bottom-left of the second monitor, but I'm not able to scale it to fit.

The warning on this line:

[self.videoWindow setFrame: screenRect];

Is: 'NSWindow' may not respond to 'setFrame'

// inside my .h file
@property (assign) IBOutlet NSWindow *videoWindow;


// inside my .m file
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];

    NSRect videoPreviewScreenRect;
    NSArray *screenArray = [NSScreen screens];

    //Using index of 1, to get secondary monitor
    NSScreen *videoPreviewScreen = [screenArray objectAtIndex: 1];

    NSRect screenRect = [videoPreviewScreen frame];
    videoPreviewScreenRect = [videoPreviewScreen visibleFrame];

    // Get and set the screen origin based on the second monitors origin
    NSPoint videoScreenOrigin ;
    videoScreenOrigin.x = videoPreviewScreenRect.origin.x;
    videoScreenOrigin.y = videoPreviewScreenRect.origin.y;
    [self.videoWindow setFrameOrigin: videoScreenOrigin];

    // **** THIS LINE DOESN'T WORK ****
    [self.videoWindow setFrame: screenRect];

    [self.videoWindow setBackgroundColor: NSColor.blackColor];
    [self.videoWindow display];
    [self.videoWindow makeKeyAndOrderFront:nil];
}

Upvotes: 1

Views: 777

Answers (1)

Photovor
Photovor

Reputation: 403

I was able to figure out what the issue was.

[self.videoWindow setFrame: screenRect];

needed to be changed to this:

[[self videoWindow] setFrame:screenRect display:YES animate:NO]; 

Upvotes: 1

Related Questions