【发布时间】:2020-01-21 11:33:17
【问题描述】:
我的项目有一个功能,我已将我的应用程序与投影仪等外部显示器连接起来。我的应用程序在两个方向上工作。我的应用程序中有 2 个窗口。 window1 是我们的默认窗口。我创建了window2,他的名字是external window。我已经创建了外部窗口,因为我不想在投影仪上显示我的整个应用程序,所以我只是在 window2 上添加了视图控制器,然后在外部显示器(投影仪)上显示 window2。
现在的问题是,当我更改应用程序的方向时,它工作正常,但 window2 不旋转。 Window2 始终以横向模式显示。我只想将window2的方向设置为与window1相同。我已经尝试了很多,但找不到任何解决方案。
请检查我的代码。我添加了如何将应用程序与外部显示器连接的代码。如果我做错了什么,请检查并帮助我。
AppDelegate.m
-(void)initExternalWindow
{
//Setup external screen window
(AppObj).externalWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
(AppObj).externalScreen = [sb instantiateViewControllerWithIdentifier:@"ExternalVC"];
UINavigationController *navController=[[UINavigationController alloc] initWithRootViewController:(AppObj).externalScreen];
navController.navigationBarHidden=YES;
(AppObj).externalWindow.opaque = NO;
(AppObj).externalWindow.rootViewController = navController;
(AppObj).externalWindow.backgroundColor = view_bg_color;
(AppObj).externalWindow.hidden = NO;
(AppObj).externalWindow.opaque = NO;
[(AppObj).externalWindow makeKeyAndVisible];
}
ViewController.m
- (void)viewDidLoad {
[self setupScreenConnectionNotificationHandlers];
}
#pragma mark- External dispaly detections (Add Notifications)
- (void)setupScreenConnectionNotificationHandlers
{
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(handleScreenConnectNotification:)
name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDisconnectNotification:)
name:UIScreenDidDisconnectNotification object:nil];
}
- (void)handleScreenConnectNotification:(NSNotification*)aNotification
{
[self setupExternalScreen];
}
- (void)handleScreenDisconnectNotification:(NSNotification*)aNotification
{
if ((AppObj).externalWindow)
{
(AppObj).externalWindow.hidden = YES;
(AppObj).externalWindow = nil;
}
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return YES;
}
外部VC.m
- (void)viewDidLoad {
[super viewDidLoad];
self.updatedImg.image = (AppObj).updatedImg;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
// UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
[UIViewController attemptRotationToDeviceOrientation];
}];
}
【问题讨论】:
-
你去看看你的类似场景stackoverflow.com/questions/6697605/…检查@Mecki解决方案,他给出了一个简单的出路,
-
@AbuUlHassan 我已经尝试了此链接的所有解决方案。但没有用。
-
@AbuUlHassan 我使用过 (github.com/rdhiggins/ExternalScreenDemo) 这个演示。并使用(airsquirrels.com/reflector)这个软件,它是作为投影仪工作的。我们只需要使用“屏幕镜像”连接我们的应用程序。如果您有时间,请检查并让我知道您是否可以提供帮助。谢谢
-
对不起,我出去了,我还没有遇到过这种问题,所以我可能不知道,即使我可以测试它,但我没有投影仪;)
-
@AbuUlHassan 好的,没有问题。但如果只是你没有投影仪,那么我之前说过你可以使用(airsquirrels.com/reflector)这个软件。你只需要下载这个软件,它会给你7天的试用期。所以你什么时候安装它,它将作为投影仪工作。
标签: ios objective-c xcode orientation uiwindow