My project has a functionality, I have connected my app with external display like projector. My application is working in both orientation. There are 2 window in my app. window1 is our default window. I have created window2, his name is external window. I have created external window, because I do not want to show my entire application on projector, so I just added view controller on window2 and then display window2 on external display(projector).
Now problem is that, when I change orientation of my application its working fine, but window2 does not rotate. Window2 is always displayed on landscape mode. I just want to set orientation of window2 same as window1. I have tried a lot but could not find any solution.
Please check my code. I have added code how to connect app with external display. please do check and help me if i have done any thing wrong.
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;
}
ExternalVC.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];
}];
}