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

How to force orientation of all screen to be in portrait mode if switch is on ?? if user rotate it should not change its orientation

这是一个超级class文件名Apporientationviewcontroller.h/m。我在所有其他子 class 中调用这个超级 class。所以如果“isPortraitModeONN”是“ON” 那么所有的屏幕都只能在纵向模式下工作。如果用户尝试将设备更改为横向,则不应旋转。如果开关是“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];
    }
}

转到目标 -> 部署信息 -> 将设备方向设置为仅纵向

作为你的问题,如果你想通过点击按钮来做到这一点 请试试这段代码,我不确定它是否适用于你的项目,但它适用于我的虚拟项目 请试一试

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

更改两个按钮的设备方向

你的代码应该是这样的,

AppDelegate.h

@property () BOOL restrictRotation;
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;

AppDelegate.m

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
   if(self.restrictRotation)
   return UIInterfaceOrientationMaskLandscape; //you can change upsidedown etc
else  
   return UIInterfaceOrientationMaskPortrait; // you can change landscape right or left only

 //this should be your initial app orientation because by default value of bool is false
}

SettingsViewController(您要从中更改整个应用的方向)

-(void) restrictRotation:(BOOL) restriction
{
   AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
   appDelegate.restrictRotation = restriction;
   [appDelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

别忘了导入 appdelegate.h ;)

最后从你改变方向的开关,

[self restrictRotation:YES]; // or NO

您可以根据需要操作 supportedInterfaceOrientationsForWindow,就像您可以将 属性 设置为 int 而不是 bool,并且可以为不同的 int 值设置多个方向。

希望这会有所帮助:)

你可以这样做-

  1. AppDelegate.h class 中添加 - @property () BOOL isPortraitModeONN;
  2. 现在在 AppDelegate.m -

    中编码
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
         self.isPortraitModeONN=YES;
    
         return YES;
     }
    
    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
        {
    
            if(self.isPortraitModeONN)
                return UIInterfaceOrientationMaskPortrait;
            else
                return UIInterfaceOrientationMaskLandscape;
    
        }
    
  3. 现在添加 IBActionUIButton 用于在 Apporientationviewcontroller.m--

    中更改方向
    - (IBAction)changeOrientation:(id)sender {
    
        if (allowedToRotate) {
    
            allowedToRotate=NO;
    
            AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    
            appDelegate.isPortraitModeONN = NO;
    
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
           }else{
    
            allowedToRotate=YES;
    
            AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    
            appDelegate.isPortraitModeONN = YES;
    
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
        }
       }
    

Apporientationviewcontroller.m中定义BOOL allowedToRotate;默认值为YES

这段代码对我有点用..

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