How to make the orientation of the entire screen be in portrait mode if the switch is on? if the user is turning, he should not change orientation

This is the name of the superclass file Apporientationviewcontroller.h/m . And I call this superclass in all other subclasses. Thus, if " isPortraitModeONN " is set to " ON ", then the entire screen should only work in portrait mode. if the user is trying to change the device to landscape, he should not rotate. It should always be in portrait mode if the switch is " ON ". In my case, laughing at the application, it is in portrait mode. but if I rotate the screen, it changes towards the landscape. but it should not change orientation until the OFF switch is set.

  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]; } } 
+5
source share
4 answers

This code works for me somewhat.

  -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator: (id<UIViewControllerTransitionCoordinator>)coordinator { ///if isPortraitMode On then force the orientation to portrait if it 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]; } 
+3
source

Go to goal β†’ Deployment information β†’ set device orientation to portrait only

enter image description here

As your question, if you want to do this by clicking on the button Please try this code, I'm not sure if it works on your project, but it works for my dummy project try

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

change the device orientation for both buttons

0
source

The code should be such

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 (where do you want to change the orientation of the entire application)

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

don't forget to import appdelegate.h;)

And finally, from your switch to change orientation,

 [self restrictRotation:YES]; // or NO 

You can manipulate supportedInterfaceOrientationsForWindow as required, for example, you can set the property as int instead of bool, and you can set multiple orientation for different int values.

hope this helps :)

0
source

You can do this:

  • Add - @property () BOOL isPortraitModeONN; in the AppDelegate.h class .
  • Now enter the code in 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; } 
  • Now add IBAction of UIButton to change orientation in 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"]; } } 

define Apporientationviewcontroller.m BOOL allowedToRotate; the default value is YES ;

0
source

Source: https://habr.com/ru/post/1247678/


All Articles