0

i want to change the device orientation on button click without rotating the device.

HERE IS MY CODE:

-(void)funcLandscapeBtnTapped:(UIButton *)button
{ 
      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
}

Thanks in advance.

kalpesh satasiya
  • 799
  • 8
  • 18

2 Answers2

0

try this code, its working,

Your's Button Action:

isEnablePortraidMode = true;
NSNumber *number = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
NSNumber *StatusBarOrientation = [NSNumber numberWithInt:UIInterfaceOrientationMaskLandscapeLeft];
[UIViewController attemptRotationToDeviceOrientation];
[[UIDevice currentDevice] setValue:number forKey:@"orientation"];
[[UIApplication sharedApplication] performSelector:@selector(setStatusBarOrientation:) withObject:StatusBarOrientation];
[UIViewController attemptRotationToDeviceOrientation];

and Add this method in Appdelegate

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if(isEnablePortraidMode)
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
     }
    else
    {
         return UIInterfaceOrientationMaskPortrait;
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Som Nai
  • 91
  • 1
  • 6
0

Try this one

AppDelegate.h

@property BOOL restrictRotation;

AppDelegate.m

#define APPDELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])

- (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (self.restrictRotation == YES) {
        return UIInterfaceOrientationMaskLandscape;        
    }
    else {
        if (isiPhone || isiPod) {
            return UIInterfaceOrientationMaskPortrait;
        }else{
            return UIInterfaceOrientationMaskLandscape;
        }
    }
}

YourViewController.m

-(void)funcLandscapeBtnTapped:(UIButton *)button
{
    APPDELEGATE.restrictRotation = YES;
        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
                                    forKey:@"orientation"];
        [self shouldAutorotate];
}
- (BOOL)shouldAutorotate {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if ( orientation == UIInterfaceOrientationLandscapeRight
        | orientation == UIInterfaceOrientationLandscapeLeft)
    {
        return NO;
    }

    return YES;
}
Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
  • Now how can i get default full screen button action in AVPlayerviewcontroller? It comes when we showsPlaybackControls = true. – Wasim Makwana Dec 23 '17 at 11:16