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
Asked Answered
Y

4

6

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];
    }
}
Yarbrough answered 22/4, 2016 at 4:50 Comment(0)
Y
3

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];
 }
Yarbrough answered 10/5, 2016 at 11:52 Comment(0)
W
1

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;

Watercraft answered 22/4, 2016 at 7:9 Comment(0)
B
0

Goto Target -> Deployment info -> set device Orientation to portrait only

enter image description here

As par your question if you want to do this by tapping on button Please try this code i am not sure about this it works on your project but it works for my dummy project please try it

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

change the device orientation for both buttons

Bronny answered 22/4, 2016 at 4:52 Comment(3)
Thanks for your comment..if i follow this procedure then app will work only in portrait mode .. But app my app should also support for landscape orientation..If Portrait Switch is "OFF" then it should work only in landscape mode.. My process is (i) if Portrait Switch is "ON" then app should work in portrait mode only if user rotate screen it should not change its orientation. (ii) if Portrait Switch is "OFF" then app should work in landscape mode only if user rotate screen it should not change its orientation.Yarbrough
Still Orientation getting changed.. if i rotate screen in simulatorYarbrough
i have already done this in my setting window that is working fine..My issue is not this.. After setting the flag "ON" in settings window, all the screen should work in portrait mode..if user change orientation it should not change.. if flag is "off" then app screen should work only in landscape.Yarbrough
D
0

You code should be like this,

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 (from where you want to change whole app orientations)

-(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 for changing orientation,

[self restrictRotation:YES]; // or NO

You can manipulate supportedInterfaceOrientationsForWindow as per requirement like you can set property as int instead of bool and can set multiple orientation for different int value.

hope this will help :)

Discontinue answered 22/4, 2016 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.