0
votes

Hello I am quite new to iPhone development (working with iOS 7). Basically I want my application to respond to screen rotations (i.e. changing the screen orientation) My project uses both UITabBarController and UINavigationController. However when I rotate the device it won't call the "shouldAutorotate" function in a ViewController called LoginView.m.

Therefore I have followed the answer in here up to the point where it subclasses both UITabBarController and UINavigationController. Can someone explain to me how to add (i.e reference it)the orientation behaviour within the LoginView.m class or to the entire project.

Your help is greatly appreciated.

Thanks

3
Anyone? Please help me..Yrol
please elaborate a bit more what you are trying to achieve. it is correct that the shouldAutorotate will be called for the root view controller, which in your case, is a TabBarController or a NavigationController, but we need more details on the desired outcomeArgent
@Argent Basically I want to change the screen orientation respect to the screen rotation. I have a view called LoginView.m which extends "UIViewController". Also I see UITabBarController and UINavigationController properties in my AppDelegate.m. Please helpYrol
if you want your views to autorotate and you are using basic ios controls that should work out of the box. just select the device orientations you want to support in the project file of your app in xcodeArgent
@Argent, I already did that, by including Portrait (bottom home button), Portrait (top home button), Landscape (left home button) and Landscape (right home button) in the plist file but no success. Or is there any other way?Yrol

3 Answers

0
votes

In your subclassed UItabbarcontroller

- (BOOL)shouldAutorotate {

    UINavigationController *navView = (UINavigationController *)[self selectedViewController];
    //Get the selected current tab viewcontroller. I guess you are having a Navigationcontroller    

    UIViewController *vc = [navView.viewControllers objectAtIndex:0];  
    //Fetch root viewcontroller of your navigationcontroller

    return [self checkOrientationForViewController:vc];

}

-(BOOL) checkForViewsForViewController : (UIViewController *)vc{

    if([vc isKindOfClass:[FirstViewController class]]){

        FirstViewController *vc1 = (FirstViewController *)vc;

        return [vc1 shouldAutorotate];
    }
    if([vc isKindOfClass:[SecondViewController class]]){

        SecondViewController *vc2 = (SecondViewController *)vc;

        return [vc2 shouldAutorotate];
    }
    if([vc isKindOfClass:[ThirdViewController class]]){

        ThirdViewController *vc3 = (ThirdViewController *)vc;

        return [vc3 shouldAutorotate];
    }
    return YES;
}

In respective viewcontrollers implement the shouldAutorotate method

0
votes

Instead of using Delegation you can also use

- (NSUInteger)supportedInterfaceOrientations

UINavigationController and UITabBarController ask their child view controllers which interface orientations they support. So you can implement "- (NSUInteger)supportedInterfaceOrientations" in your LoginView.m and return the appropriate interface orientations.

You also need to edit your Info.plist and add the supported interface orientations their. You can do this with Xcode by opening the project view. Also have a look at Apples documentation on supporting interface orientations

0
votes

Instead of subclassing, is worth to know that there are also delegate methods, in UINavigationcontroller and UITabbarController that make you handle rotations at runtime:

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController

Ref doc.

- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController

Ref doc.

Evenf if Apple has removed the advice to do not subclass Nav and Tabbar controller, using delegation will be the most reliable solution over time