0

Based on the discussing on Apple's forum, this method works great on iOS < 11 device (But > iOS 6)

This will prevent any rotation.

- (BOOL)shouldAutorotate
{
    return NO;
}

This will only rotate to portrait (right-side-up or up-side-down).

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait + UIInterfaceOrientationMaskPortraitUpsideDown;
}

However, it's not working on iOS 11 Beta 9;

If you have an app that allow all orientations but want to set MainViewController to Portrait only by using this.

If you hold your device landscape and start the app, after a launching screen, the statusBar will be on the top of screen (when landscape) and your view still portrait.

If you use method to switch rootViewController like this answer, you will end up a landscape rendered view that force showing in portrait.

Some example graph like this one:

enter image description here

The Gear Icon is at correct location but that was from a library. Everything else looks like landscape.

Thanks

Sparga
  • 1,517
  • 12
  • 21
SharkIng
  • 178
  • 1
  • 13

2 Answers2

1

Not enough reputation points to reply to @Vishal16's answer... it helped me.

I wanted to allow all orientations on all view controllers on iPad and portrait for most view controllers on iPhone, and I wanted it in swift, so here is my slightly modified version:

In your app delegate:

var isPortrait = true

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

    if UIDevice.current.userInterfaceIdiom == .pad || !isPortrait {
        return .all
    }
    return .portrait
}


Then in prepare(for segue:...

    (UIApplication.shared.delegate as? AppDelegate)?.isPortrait = true

Use false for rotating view controllers on a segue, true for others.

Enjoy!

Community
  • 1
  • 1
Jeroen
  • 41
  • 1
  • 4
0

When you will present any viewController AppDelegate automatically call this method for forced rotation. I am using this and it's work just flawless in my All Apps

AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
   if (self.isPotrait) {
       return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
   }
   return UIInterfaceOrientationMaskAll;
}

Define a property :

@property (assign, nonatomic) BOOL isPotrait;

In prepareForSegue method call like this:

if ([segue.identifier isEqualToString:@"segue_ID"]){
     AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
     [appDelegate setIsPotrait:true];
     YourViewController *yourVC = [segue destinationViewController];
}

Hope this help you. Happy codding :)

iamVishal16
  • 1,780
  • 18
  • 40
  • `supportedInterfaceOrientationsForWindow` seem works fine even without `self.isPotrait`, but this call set all Window to Portrait. We are loading an Interstitial ad and we would like it showing in Landscape. Our test shows that ads won't able to show in Landscape if we set `OrientationsForWindow` to Portrait. – SharkIng Sep 11 '17 at 16:45
  • you can toggle your desired orientation using isPotrait flag. If you want to return AllMask or any specific. Then please play this flag when you popback or dismiss your viewController set this flag accordingly. – iamVishal16 Sep 12 '17 at 04:59