0

我正在尽力将我的 iPad 应用程序屏幕镜像到外部显示器。我知道它只是在选择 Apple TV 后使用 AirPlay 和 Mirror 自动执行此操作,但是,该方法总是使其成为信箱,并且不会占用整个屏幕。我只是想让它反映 iPad 上的确切内容,但要占据整个 Apple TV 屏幕。在 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;

在我的项目的常规设置下,我只检查了横向左侧的方向。该应用程序是仅 iPad 应用程序,使用 MainWindow.xib 并且没有情节提要。所有的 ViewController 都在 MainWindow.xib 中,没有一个类有自己的 .xib 文件。当我 AirPlay 或使用模拟器外接显示器时,iPad 和外接显示器屏幕如下所示: 在此处输入图像描述 在此处输入图像描述 如您所见,外接显示器镜像似乎工作,因为它显示与导航栏相同的紫色,但可能是方向是错的吗?我不允许在任何视图控制器中自动旋转,也没有更改任何设置。我究竟做错了什么?

4

1 回答 1

0

我认为问题出在这里:

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

您在不设置 rootViewController 的情况下初始化导航视图控制器。您应该设置 tabBarController.rootViewController = _remoteVC;

于 2015-10-14T12:03:54.410 回答