У моего проекта есть функциональность, я подключил свое приложение к внешнему дисплею, например к проектору. Мое приложение работает в обеих ориентациях. В моем приложении есть 2 окна. window1 — наше окно по умолчанию. Я создал окно2, его имя внешнее окно. Я создал внешнее окно, потому что я не хочу показывать все свое приложение на проекторе, поэтому я просто добавил контроллер просмотра в окно2, а затем отобразил окно2 на внешнем дисплее (проекторе).
Теперь проблема в том, что когда я меняю ориентацию своего приложения, оно работает нормально, но окно2 не вращается. Window2 всегда отображается в ландшафтном режиме. Я просто хочу установить ориентацию окна2 так же, как окно1. Я много пробовал, но не мог найти решения.
Пожалуйста, проверьте мой код. Я добавил код, как подключить приложение к внешнему дисплею. Пожалуйста, проверьте и помогите мне, если я сделал что-то не так.
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;
}
ВнешнийВК.м
- (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];
}];
}