IOS外部显示以编程方式
-
13-12-2019 - |
题
我正在尝试在iOS应用程序上支持外部显示而不使用任何.xib文件。
现在我的代码包含:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([[UIScreen screens] count] > 1){
externalScreen = [[UIScreen screens] objectAtIndex:1];
UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:0];
externalScreen.currentMode = current;
externalWindow = [[UIWindow alloc] initWithFrame:[externalScreen bounds]];
externalWindow.screen = externalScreen;
externalWindow.clipsToBounds = YES;
extController = [[ExternalController alloc] init];
[externalWindow addSubview:extController.view];
[externalWindow makeKeyAndVisible];
}
self.window = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:0] bounds]];
mainController = [[ViewController alloc] init];
[self.window addSubview:mainController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
}
.
当我运行这个时,电视屏幕将立即关闭。 如果我发表评论这个行:
//self.window = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:0] bounds]];
.
电视屏幕将正常工作,但当然我没有在我的模拟器屏幕上看到任何东西。
有没有人知道我应该做什么?提前谢谢!
解决方案
我可以建议您在github上尝试采样源代码TVOUT - https://github.com/johngoodstadt/tvout
它使用与您的类似代码,但是包装在您可以复制的类中并从您自己的代码中调用。
它应该解决您的显示问题。顺便说一下,我不会尽早尽早完成代码,而是在第一个视图控制器中的“didfinishlaunchingwithoptions”中。
不隶属于 StackOverflow