IOS 6: how to force the orientation of the change when you press the view controller on the navigation controller stack

I think this question should have been asked a million times, but I still can’t find the answer to this question.

Here is my hierarchy: UINavigationController -> UIViewController 1 -> (push) -> UIViewController 2

UINavigationController: supports all possible orientations UIViewController 1: only supports portrait UIViewController 2: only supports landscape

How can I block UIViewController 1 only in portrait and at the same time block UIViewController 2 only in landscape? Is it possible? So far, I see that UIViewController 2 always takes the orientation of UIViewController 1.

Please note that this is only for iOS 6.

Thanks!

+4
source share
3 answers

I also found the same problem. I found that the shouldAutorotate function does not call every time, so I change the orientation of the programmatic

import this first

#import <objc/message.h> 

then

 -(void)viewDidAppear:(BOOL)animated { if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft ); } } } 

Hope this helps you.

+13
source

Add a new Objective-C class (subclass of UINavigationController) and add the following code to the .m files

 -(NSUInteger)supportedInterfaceOrientations { NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]); return [self.topViewController supportedInterfaceOrientations]; } -(BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // You do not need this method if you are not supporting earlier iOS Versions return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } 

After adding new classes, go to ViewController classes and make the following changes

 - (BOOL)shouldAutorotate // iOS 6 autorotation fix { return YES; } - (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix { return UIInterfaceOrientationMaskAll; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix { return UIInterfaceOrientationPortrait; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } 

enter image description here

In shouldAutorotate, shouldAutorotateToInterfaceOrientation: return YES if you want the ViewController to support the multiple orientation else return NO, also in the houldAutorotateToInterfaceOrientation: the method passes the Orintation you want for this particular ViewController, repeat the same for all view controllers.

The reason of that: -

1: Although you can change the preferred InterfaceOrientationForPresentation: of any viewController to a specific orientation, but since you are using the UINavigationController, you also need to override the supported interfaces for your UINavigationController

2: To override the supported interfaces for the UINavigationController, we subclassed the UINavigationController and modified the method associated with the UINavigation orientation.

Hope this helps you!

+9
source

Make the application only support portrait mode and add the following line to initWithNibName UIViewController 2

 self.view.transform = CGAffineTransformMakeRotation(M_PI/2); 
+1
source

Source: https://habr.com/ru/post/1479092/


All Articles