NSScreen returns nil

138 Views Asked by At

I am trying to get the screen width and height, but NSScreen returns nil and width & heigh 0. Why is it so? According to the documentation NSScreen may return nil if there are no available screens, or it is completely off screen. But the window looks fine. This only happens when I use a scaling factor smaller than 100% for my window (e.g. 50%).

NSView* view = (NSView*) frame->getPlatformFrame()->getPlatformRepresentation();
NSWindow* window = [view window];
NSScreen *myScreen = [window screen];
double width = myScreen.frame.size.width;
double height = myScreen.frame.size.height;

When I directly use mainScreen, I get a valid object with the current width & height of the screen:

double width = [[NSScreen mainScreen] frame].size.width;
double height = [[NSScreen mainScreen] frame].size.height;

Any ideas? Thanks!

1

There are 1 best solutions below

0
Marek H On

NSScreen will return nil for NSWindow if the window coordinates do not belong to any screen. This can happen if you manually restore window frame and the attached screen/monitor was disconnected .