iOS External Display Incorrect Orientation

375 views Asked by At

I am trying my best to mirror my iPad app screen to an external display. I know that it does this automatically just by using AirPlay and Mirror after selecting Apple TV, however, that method always makes it letterbox, and doesn't take up the entire screen. I simply want it to mirror the exact content from the iPad, but take up the entire Apple TV Screen. In code for AppDelegate.h:

#import <UIKit/UIKit.h>
@class MainView;
@class ViewController2;
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *tabBarController;
    ViewController2 *_remoteVC;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *tabBarController;
@property (strong, atomic) UIWindow *secondWindow;
@end

AppDelegate.m:

@implementation AppDelegate
@synthesize tabBarController;
@synthesize window, secondWindow;

- (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...
        NSLog(@"Setting up second screen: %@", secondScreen);
        _remoteVC = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
        tabBarController = [[UINavigationController alloc] init];
        self.secondWindow.rootViewController = tabBarController;
        [self.secondWindow makeKeyAndVisible];

        // Show the window.
        self.secondWindow.hidden = NO;
    }
}

- (void)setUpScreenConnectionNotificationHandlers {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
                   name:UIScreenDidConnectNotification object:nil];
    [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
                   name:UIScreenDidDisconnectNotification object:nil];
}

- (void)handleScreenDidConnectNotification:(NSNotification*)aNotification {
    UIScreen *newScreen = [aNotification object];
    CGRect screenBounds = newScreen.bounds;

    if (!self.secondWindow) {
        NSLog(@"Initializing secondWindow/screen in notification");
        self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        self.secondWindow.screen = newScreen;

        // Set the initial UI for the window.
        _remoteVC = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
        tabBarController = [[UINavigationController alloc] init];
        self.secondWindow.rootViewController = tabBarController;
    } else {
        NSLog(@"Second window already initialized.");
    }
}

- (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification {
    if (self.secondWindow) {
        // Hide and then delete the window.
        self.secondWindow.hidden = YES;
        self.secondWindow = nil;
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    sleep(1.5);

    [window setRootViewController:tabBarController];
    [self setUpScreenConnectionNotificationHandlers];

    [self checkForExistingScreenAndInitializeIfPresent];
return YES;

Under General Settings for my project, I have only Landscape Left checked for Orientation. The app is an iPad only app, using MainWindow.xib and no storyboards. All the ViewControllers are inside the MainWindow.xib, none of the classes have their own .xib file. When I AirPlay it or use the Simulator External Display, the iPad and External Display screens look like this: enter image description here enter image description here As you can see, it appears that the External Display mirroring works, as it shows the same color purple the navigation bar has, but maybe orientation is just wrong? I don't allow autorotation in any view controllers, and haven't changed any settings. What am I doing wrong?

1

There are 1 answers

0
Alain On

I think the problem is here:

'_remoteVC = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
    tabBarController = [[UINavigationController alloc] init];
    self.secondWindow.rootViewController = tabBarController;'

You initialize a navigation view controller without setting a rootViewController. You should set tabBarController.rootViewController = _remoteVC;