如果开关打开,如何强制所有屏幕的方向处于纵向模式?如果用户旋转它不应改变其方向

Nit*_*hya 6 objective-c device-orientation ios

它是一个超类文件名Apporientationviewcontroller.h/m.我在所有其他子类中调用这个超类.因此,如果" isPortraitModeONN "为" ON ",则所有屏幕仅应在纵向模式下工作.如果用户尝试将设备更改为横向,则不应旋转.如果开关处于" "状态,它应该始终处于纵向模式.在我的情况下,在笑的应用程序时,它处于纵向模式.但是,如果我旋转屏幕,它会改变为景观.但在设置中关闭开关之前不应改变其方向.

  if(isPortraitModeONN)
   {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
   }
   else
   {
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft){
        [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    }
    else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
    {
        [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
    }
    else{
        [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    }
}
Run Code Online (Sandbox Code Playgroud)

Nit*_*hya 3

这段代码对我来说有些作用..

 -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:   (id<UIViewControllerTransitionCoordinator>)coordinator
 {
///if isPortraitMode On then force the orientation to portrait if it's other than Portrait
///else force the orientation to landscape

if (isPortraitModeONN)
{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
         // do whatever
         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         if (UIInterfaceOrientationIsLandscape(orientation)) {
             ///App is rotating to landscape, force it to portrait
             NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
             [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
         }
     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     }];
}
else{

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
         // do whatever
         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         if (UIInterfaceOrientationIsPortrait(orientation)) {
             ///App is rotating to portrait, force it to landscape
             NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
             [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
         }
     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
     }];
    }
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
 }
Run Code Online (Sandbox Code Playgroud)