目前,我为 iPhone/iPad 制作的任何应用程序都可以通过 AirPlay 镜像到 Apple TV。但是,即使在横向模式下,它也只占据屏幕的中央部分,左右两侧为黑色。以 Real Racing HD 等应用程序的方式将其全屏播放到 AirPlay 涉及到什么?
编辑:根据建议,我已经添加了我正在使用的所有代码,而不是告诉 secondWindow 像往常一样使用相同的根视图控制器,而是设置了一个具有不同颜色的新 VC,以查看机制是否设置正确. 它们不是,因为它仍然只是进行正常的镜像,即使告诉它使用不同的 VC。
这是 AppDelegate.h
#import <UIKit/UIKit.h>
@class MainView;
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *tabBarController;
@property (nonatomic, retain) UIWindow *secondWindow;
@end
以及 AppDelegate.m 的相关部分
- (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);
ViewController *mainView = [[ViewController alloc] init];
self.secondWindow.rootViewController = mainView;
[self.secondWindow makeKeyAndVisible];
// Show the window.
// self.secondWindow.hidden = NO;
}
NSLog(@"Screen count too low");
}
- (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.
ViewController *mainView = [[ViewController alloc] init];
self.secondWindow.rootViewController = mainView;
} 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 {
[window setRootViewController:tabBarController];
[self setUpScreenConnectionNotificationHandlers];
[self checkForExistingScreenAndInitializeIfPresent];
return YES;
}