UPDATE 2
In my subclass of UITabBarController, I tried adding this:
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; }
Now, every time I select a tab element, the device works fine in portrait mode. However, now I can rotate the device while (a) is selected, and the device will rotate in landscape mode. How can I stop the rotation of the device?
I believe this method in my subclass of the tab controller calls this:
- (BOOL)shouldAutorotate { if(self.selectedIndex == 0) { return NO; } return [self.viewControllers.lastObject shouldAutorotate]; }
If I return 'NO', I cannot rotate when I am in the view controller, but when I select it, it does not automatically rotate to the portrait. If I return โYESโ, I can rotate when I am in the view controller, but when I select it, it automatically rotates to the portrait.
I have a custom tab bar controller in my application with the following hierarchy:
UITabBarController | UINavigationController | | | UIViewController(a) | UINavigationController | | | UIViewController(b) | UINavigationController | UIViewController(c)
I want View Controller (a) to be displayed only in portrait mode, and View Controllers (b) and (c) to be displayed in all directions, but upside down. Now I can do this with each view controller separately, but my problem arises when I am in (b) in landscape mode and select a tab element for (a) and then displayed in landscape mode that does not look good. How can I make sure the tab bar (or view controller) checks to see if the current view controller can be viewed in its current orientation?
If necessary, here is my code for (a), which itself restricts it to portrait mode:
- (BOOL) shouldAutorotate { return NO; } - (NSUInteger) supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
UPDATE
I added this to view controller (a), but I will get a black square in the middle of my view, and the title in the navigation bar is no longer centered.
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; }