我正在尽我所能将我的iPad应用程序屏幕镜像到外部显示器。我知道它只是在选择Apple TV后使用AirPlay和Mirror自动执行此操作,但是,该方法总是使其成为letterbox,并且不占用整个屏幕。我只是希望它能够反映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,没有故事板。所有ViewControllers都在MainWindow.xib中,没有一个类有自己的.xib文件。当我AirPlay或使用模拟器外部显示器时,iPad和外部显示屏幕如下所示: 正如您所看到的,外部显示器镜像似乎有效,因为它显示了导航栏所具有的相同颜色紫色,但可能方向错误?我不允许在任何视图控制器中进行自动旋转,并且没有更改任何设置。我做错了什么?
答案 0 :(得分:0)
我认为问题在于:
'_remoteVC = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
tabBarController = [[UINavigationController alloc] init];
self.secondWindow.rootViewController = tabBarController;'
初始化导航视图控制器而不设置rootViewController。 你应该设置tabBarController.rootViewController = _remoteVC;