All week I have been searching SO for a solution to my unique problem, but nothing has worked for me yet. I have a TabBarController that has 6 tabs. iOS automatically creates the "More" tab, which contains the view controller,"Dashboard", that I am having difficulty with. The "Dashboard" view controller is acting as a page controller that has 3 pages.
When I press the "Dashboard" item, I want the screen orientation to change from Portrait to Landscape. I've learned through countless other SO posts that rotation is controlled from the parent TabBarController. I have also learned that the "More" tab is actually a navigation controller, which adds some more complication to the mix.
To reiterate and be direct, when I press the "Dashboard" tab in the TabBarController's "More" tab, I want the screen to change its orientation from Portrait to Landscape.
And YES, I have already tried this: [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]forKey:@"orientation"];
and it didn't work.
I've also tried overriding every function in the TabBarController and MoreNavigationController that has to do with rotation and orientation, as well as in the child views. I will post my TabBarController below. Any help will be appreciated, thanks.
TabBarViewController.h:
@interface TabBarViewController : UITabBarController<UINavigationControllerDelegate>{}
@end
TabBarViewController.m:
@implementation TabBarViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moreNavigationController.delegate = self;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if([self.selectedViewController isKindOfClass:[Dashboard class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
{
if([tabBarController.selectedViewController isKindOfClass:[Dashboard class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAll;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"tabBarController didSelectViewController = %lu", (unsigned long)tabBarController.selectedIndex);
}
- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
{
NSLog(@"navigationControllerPreferredInterfaceOrientationForPresentation");
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class]])
{
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
return UIInterfaceOrientationPortrait|UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if([viewController isKindOfClass:[Dashboard class]])
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]forKey:@"orientation"];
}
}
@end
TabBarViewController
, for one reason or another, needs a trigger to rotate, but it can be done. stackoverflow.com/a/32390717/218152 shows how to achieve orientation changes onTabBarViewController
without being modal. – SwiftArchitect