Search code examples
iosobjective-ciphoneuiwindow

How to update UIWindow based on changes to another UIWindow?


The situation arises when my iPhone/iPad is to be connected to an external display.

In the normal situation, the entire device's screen gets mirrored to the external display. But, I need the screen on the external display to display content that is different from that in the device, for a specific view/page.

I have a UIImageView inside a UIScrollView, and some related buttons (next, previous, etc) as part of a specific View Controller of the app. I want the external device to display only the content in the UIScrollView (that is, the buttons are not to be shown). For this, it seems like I have to create another instance of UIWindow for the external display screen.

But, how can I make the UIWindow (and it's content) in the external display to respond correspondingly to the changes made to the main UIWindow (the one which is displayed in the iPhone/iPad). That is, changes like zooming in and zooming out.


Solution

  • You can do somthing like this:

    - (void)checkForExistingScreenAndInitializeIfPresent
    {
        if ([[UIScreen screens] count] > 1)
        {
            // Get the screen object that represents the external display.
            UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
            // Get the screen's bounds so that you can create a window of the correct size.
            CGRect screenBounds = secondScreen.bounds;
    
            self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
            self.secondWindow.screen = secondScreen;
    
            // Set up initial content to display...
            secondWindow.rootViewController = [ExternalScreenViewController sharedExternalScreen];
    
            // Show the window.
            self.secondWindow.hidden = NO;
        }
    }
    

    Now all you need to do is create the ExternalScreenViewController with a UIView that fills the entire viewController. Finally you just set the view of the target equal to the view of the scrollView.