Setting UIScreen's mode / resolution

丶灬走出姿态 提交于 2019-12-06 11:36:19

问题


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:


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.



来源:https://stackoverflow.com/questions/19810964/setting-uiscreens-mode-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!