Setting UIScreen's mode / resolution

2019-04-13 13:00发布

I have to set the external UIScreen's mode to run with the resolution 1024x768. First I search if the screen supports this resolution:

if ([[UIScreen screens] count] > 1){

    CGSize size1024;
    size1024.height = 0;
    size1024.width  = 0;
    UIScreenMode *screenMode1024 = nil;
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];

    for(int i = 0; i < [[secondScreen availableModes] count]; i++)
    {
       UIScreenMode *current = [[[[UIScreen screens] objectAtIndex:1] availableModes] objectAtIndex: i];
       if (current.size.width == 1024.0 && current.size.height == 768.0)
       {
           size1024 = current.size;
           screenMode1024 = current;
           break;
       }
    }
}

After that I set the external screen's mode to use this resolution, but somehow it does not work and the screen is using other, the default resolution.

secondScreen.currentMode = screenMode1024;
UIWindow *secondWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0,0, size1024.width, size1024.height)];
secondWindow.screen = secondScreen;

...


secondWindow.hidden = NO;

Any help ? Maybe I missed some settings ? I also tried with this :

[[[UIScreen screens] objectAtIndex:1] setCurrentMode:screenMode1024];

1条回答
叼着烟拽天下
2楼-- · 2019-04-13 13:52

I found the solution. The problem was that the screen's mode has to be changed when the external screen is connected to the iOS device.

[[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(screenDidConnectNotification:) name: UIScreenDidConnectNotification object: nil];

The screen mode should be changed inn the screenDidConnectNotification function.

查看更多
登录 后发表回答