Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

It is an superclass file name Apporientationviewcontroller.h/m. And i am calling this super class in all other subclasses. So that if "isPortraitModeONN" is "ON" then all the screen should work only in portrait mode. if user try to changes the device to landscape it should not rotate. it should be always in portrait mode if switch is "ON".. In my case while laughing the app it is in portrait mode. but if i rotate the screen it is changing towards landscape. but it should not change its orientation until turning OFF the switch in settings..

  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];
    }
}
like image 922
Nithya Avatar asked Apr 22 '16 04:04

Nithya


2 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'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];
 }
like image 58
Nithya Avatar answered Oct 06 '22 00:10

Nithya


You can do like this-

  1. Add - @property () BOOL isPortraitModeONN; in AppDelegate.h class.
  2. Now code this 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;
    
        }
    
  3. Now Add IBAction of UIButton for changing 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 in Apporientationviewcontroller.m BOOL allowedToRotate; default value is YES;

like image 45
Rohit Khandelwal Avatar answered Oct 05 '22 23:10

Rohit Khandelwal