Paul T.
Paul T.

Reputation: 5038

Issue with orientations of 2 UIViewcontrollers in navigation controller application

I have a tabbar application. And the item of first tab is Navigation Controller. Navigation controller has 4 items in his stack. I want to provide rotation. But in tabbar application it's the problem, that's why I created my own tabbarcontroller and override the method:

@interface RotatingTabBarController : UITabBarController

@end

@implementation RotatingTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if([self.selectedViewController isKindOfClass:[UINavigationController class]]){
        BOOL f = [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
        return f;
    } else {
        BOOL f = [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
        return f;
    }
}

@end

After that if I provide in proper UIInterfaceOrientation support, my controllers will support autorotation. But without my custom RotatingTabBarController it's seems impossible

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

The problem is: When I push FirstViewController in navigation controller in shouldAutorotateToInterfaceOrientation of this viewcontroller I provide only portrait orientation, but when I push SecondViewController (I provide there both portrait and landscape orientation), if current interface orientation of SecondViewController is landscape and I press back button (SecondViewController pop from stack and FirstViewController appears), the orientation of FirstViewController is landscape. But in shouldAutorotateToInterfaceOrientation method I provide only portrait orientation for him.

Upvotes: 1

Views: 476

Answers (2)

Paul T.
Paul T.

Reputation: 5038

I used modal windows and hide back button in Landscape orientation. It solved the issue.

Upvotes: 0

Mert
Mert

Reputation: 6065

Actually you should not subclass a UITabbarController class.

The UITabBarController class implements a specialized view controller that manages a radio-style selection interface. This class is not intended for subclassing.

From UITabBarController Class Reference

If you view controllers override

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

method, it should be fine.

I think it is not a good idea to support for some views landscape orientation and for some not in a app.

You have already the reason discovered, what if users presses back button while in landscape mode?

What should be done here, rotate to portait while the device has landscape orientation?

If you want to do so, there is no easy way to force the device to change the orientation. You have to handle the orientations of views your self using transformations.

You have possibility to support different orientation for a view using model view controller, which is not on stack of a navigation controller.

Upvotes: 3

Related Questions