[UIWindow当设备方向改变时不会旋转

问题描述 投票:0回答:1

我的项目具有功能,我已将我的应用程序连接到外部显示器(例如投影仪)。我的应用程序在两个方向上都可以工作。我的应用程序中有2个窗口。 window1是我们的默认窗口。我已经创建了window2,他的名字叫外部窗口。我已经创建了外部窗口,因为我不想在投影仪上显示我的整个应用程序,所以我只是在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;
}

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


}
ios objective-c xcode orientation uiwindow
1个回答
0
投票

您现在随旋转窗口一起去,当窗口旋转时,您也必须旋转图像。根据您的需要进行其他更改,我给了您一个带有设备旋转的旋转窗口,只是用以下代码替换了ExternalVC.m

    //
//  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
© www.soinside.com 2019 - 2024. All rights reserved.