So I have the following hierarchy:
UINavigationController
--> RootViewController (UIViewController
) --> UITableViewController
--> DetailViewController (UIViewController
)
I want to lock the orientation on RootViewController to Portrait only, but leave all orientations for the rest view controllers.
If I put this to subclassed UINavigationController
:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
All view controllers are then locked to portrait.
My question is, is there a way to lock only RootViewController to Portrait, but leave all options for other view controllers?
in singleton
-(void)setOrientationSupport:(BOOL)flag{
flag_orientationSupport_ = flag;
}
-(BOOL)getOrientationSupport{
return flag_orientationSupport_;
}
in appdelegate
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([singleton getOrientationSupport])
return UIInterfaceOrientationMaskAll;
else
return UIInterfaceOrientationMaskPortrait;
}
add the following code in viewwillappear
for the viewcontroller you want support orientation
[singleton setOrientationSupport:YES];
to those controller you want disable orientation
[singleton setOrientationSupport:NO];
check the link here for fixing autorotation in iOS 6 and set orientation support per view basis: http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/
Here is what you could do:
Create a custom navigation controller that is a subclass of UINavigationController
, in your .m file:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
In your AppDelegate.h
,
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
UINavigationController *navigationController;
ViewController *viewController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
and in AppDelegate.m
,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// set initial view
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
navigationController = [[CustomNavigationController alloc]
initWithRootViewController:viewController]; // iOS 6 autorotation fix
[navigationController setNavigationBarHidden:YES animated:NO];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController]; // iOS 6 autorotation fix
//[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix
{
return UIInterfaceOrientationMaskAll;
}
in your rootViewController, for whatever the event push the second view controller, do this:
- (IBAction)pushSecondViewController:(id)sender {
// push second view controller
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
in your each view controller, add
- (BOOL)shouldAutorotate{}
- (NSUInteger)supportedInterfaceOrientations{}
for iOS 6, you can set each view controller whatever the orientation support you want individually.
for iOS 5 and below, you can set
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
All the credits goes to John DiSalvo who wrote the sample app in the link.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With