开发者社区 问答 正文

关于ios转屏问题,某个界面需要支持转屏

我们在appdelegate 里面用代码把屏幕转向禁止
(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; } 现在想要某一个界面支持转屏后横屏(UIInterfaceOrientationMaskPortrait不能改,其他界面要求竖屏)

展开
收起
爵霸 2016-03-13 11:22:52 2248 分享 版权
1 条回答
写回答
取消 提交回答
  • //Appdelegate.h
     @property(nonatomic,assign)BOOL Orientations;
     //Appdelegate.m
    (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if(Orientations) { return UIInterfaceOrientationMaskAllButUpsideDown; } return UIInterfaceOrientationMaskPortrait;
    
    }
    
    //要旋转的页面中
     UIDevice *device = [UIDevice currentDevice]; //Get the device object
     [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
     [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
    
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
     delegate.Orientations=YES;
    
    (void)orientationChanged:(NSNotification *)note {
     UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
    
    switch (o) {
     case UIDeviceOrientationPortrait: // Device oriented vertically, home button on the bottom
        break;
    case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top
    
        break;
    case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
    
        break;
    case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
    
        break;
    default:
        break;
    
    
    }
     }
    
    
    (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
     {
     // 操作
     return YES; // YES为允许横屏,否则不允许横屏
     }

    希望对你有帮助。

    2019-07-17 19:02:16
    赞同 展开评论
问答分类:
问答标签:
问答地址: