3

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];
}];


}
Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • here you go for your similar scenario https://stackoverflow.com/questions/6697605/iphone-uiwindow-rotating-depending-on-current-orientation check @Mecki solution he have given an simple way out, – Abu Ul Hassan Sep 20 '19 at 12:09
  • @AbuUlHassan I have tried all solutions of this link. but did not work. – Monika Patel Sep 20 '19 at 12:50
  • @AbuUlHassan I have used (https://github.com/rdhiggins/ExternalScreenDemo) this demo. And used (https://www.airsquirrels.com/reflector) this software, it is work as projector. We just have to connect our app using "screen mirroring". If u have time please check and let me know if u can help in that. thanks – Monika Patel Sep 20 '19 at 12:59
  • sorry i was out i have not gone through this kind of issue so may be i have no idea even i could have tested it on but i dont have a projector ;) – Abu Ul Hassan Sep 23 '19 at 09:18
  • @AbuUlHassan Okay no prob. but If it is just about u don't have projector then I said u before that u can use (https://www.airsquirrels.com/reflector) this software. you just need to download this software, it will give u 7 days trial period. so when will u install this it will be work as projector. – Monika Patel Sep 23 '19 at 09:37
  • okay let me try – Abu Ul Hassan Sep 23 '19 at 10:01
  • @AbuUlHassan thanks .. I will wait for your reply. – Monika Patel Sep 23 '19 at 10:02
  • okay now how i can connect it with my ipad ? or how i can use it with? mac? – Abu Ul Hassan Sep 23 '19 at 10:06
  • you just need to start that reflector software and then turn on "Screen mirroring" from your iPad. – Monika Patel Sep 23 '19 at 10:12
  • okay but https://github.com/rdhiggins/ExternalScreenDemo is in swift and your code is in objective-c can place your code somewhere? – Abu Ul Hassan Sep 23 '19 at 10:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199833/discussion-between-monika-patel-and-abu-ul-hassan). – Monika Patel Sep 23 '19 at 10:32
  • see chat please. – Abu Ul Hassan Sep 23 '19 at 10:49
  • https://drive.google.com/file/d/1JUrLD4UybaOuwjanFbl4MWzwDZKfjaUW/view – Monika Patel Sep 23 '19 at 11:29
  • So second window which is external window created on basis of external screen's display Result you have to work in current window, you can add external window as place holder window select your image and then hide external window from external screen after image processing is done you can add that window again so that external viewer should not be able to see your internal app what you say? – Abu Ul Hassan Sep 25 '19 at 05:54

1 Answers1

0

Here you go with your Rotational window now you have to Rotate image too when window rotates Do your other changes as you want i have given you a rotated window with device rotation just replac ExternalVC.m with following code

    //
//  ExternalVC.m
//  Sketch
//
//  Created by mac on 21/07/18.
//  Copyright © 2018 mac. All rights reserved.
//

#import "ExternalVC.h"

@interface ExternalVC ()

@end

@implementation ExternalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.updatedImg.image = (AppObj).updatedImg;
    NSLog(@"External window bounds:%lu",(unsigned long)self.supportedInterfaceOrientations);
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(orientationDidChange:)
                   name:UIDeviceOrientationDidChangeNotification object:nil];

}

- (void)orientationDidChange:(NSNotification*)aNotification
{
      NSLog(@"orientation did change :%@",aNotification);
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
   // [self transformForOrientation:orientation];
    [self.view.window setTransform:[self transformForOrientation:orientation]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if ([UIScreen mainScreen] == (AppObj).externalWindow.screen || !(AppObj).externalWindow) {
        return UIInterfaceOrientationMaskAll;
    }else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

- (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)
     {
//
//         NSLog(@"External window bounds:%f || %f",(AppObj).externalWindow.bounds.size.width,(AppObj).externalWindow.bounds.size.height);
//    //     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//     //    [(AppObj).externalWindow setValue:@(orientation) forKey:@"orientation"];
////         [[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
////
////
////         [UINavigationController attemptRotationToDeviceOrientation];
//         [UIViewController attemptRotationToDeviceOrientation];
//          UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//        [self transformForOrientation:orientation];

     }];


}

#define DegreesToRadians(degrees) (degrees * M_PI / 180)

- (CGAffineTransform)transformForOrientation:(UIDeviceOrientation)orientation {


//        [self.view.window setFrame:CGRectMake(0, self.view.window.frame.size.height-50, self.view.window.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
//        [self.view.window makeKeyAndVisible];


    switch (orientation) {

        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"UIInterfaceOrientationLandscapeLeft****");
            return CGAffineTransformMakeRotation(DegreesToRadians(90));


        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"UIInterfaceOrientationLandscapeRightt****");
            return CGAffineTransformMakeRotation(-DegreesToRadians(90));

        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"UIInterfaceOrientationUpSideDownPortrait****");
            return CGAffineTransformMakeRotation(DegreesToRadians(180));

        case UIInterfaceOrientationPortrait:
        default:
            NSLog(@"UIInterfaceOrientationPortait****");
            return CGAffineTransformMakeRotation(DegreesToRadians(0));
    }
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSLog(@"status bar orientation :%ld",(long)orientation);
  //  [self.view setTransform:[self transformForOrientation:orientation]];

}
-(BOOL)shouldAutorotate{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
Abu Ul Hassan
  • 1,340
  • 11
  • 28