0

My app has Portrait mode only, as follow image: Device Orientation

But it needs to support Landscape mode in customized UIViewController. I have override follow functions:

    - (BOOL)shouldAutorotate
    {
        return NO;
    }

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationLandscapeRight;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }

It worked in most cases, but somehow it would acted like follow image: problem picture

There seems to have a conflict between the device orientation and MyViewController orientation.

PS: My device is iPhone 6s Plus, system is iOS 10.0, this problem also exist in iPhone 6s.

zxbeef
  • 1
  • 1

3 Answers3

0

If you are wondering for ViewController specific orientation then please refer below answer
https://stackoverflow.com/a/27010196/6325474

Community
  • 1
  • 1
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
0

Try this .

AppDelegate.h

@property () BOOL restrictRotation;

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAll; // you can change orientation for specific type . for ex landscape left
}

ViewController

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

viewDidLoad

[self restrictRotation:YES]; or NO

you need to write this line in every viewcontroller where you need to modify

for more : https://stackoverflow.com/a/25952571/3901620

Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • My project is designed to used by other developers as static library, so modify AppDelegate may not suitable. Thx all the same. – zxbeef Dec 06 '16 at 05:48
  • My code is worked well in most cases, but the MyViewController has not spin fortuitous. And I don't know why... – zxbeef Dec 06 '16 at 05:52
0

You can use following code in AppDelegate .This is only possible solution which will work Swift code

 func application(_ application: UIApplication, 
supportedInterfaceOrientationsFor window: UIWindow?) -> 
UIInterfaceOrientationMask  {

        let vc = self.window?.currentViewController()
        if vc?.isKind(of: YourDesiredVCToChangeOrientation.self) == true {
            return UIInterfaceOrientationMask.landscape;
        } else {
            return UIInterfaceOrientationMask.all ;
        }

    }