0

[I have searched Stackoverflow and tried the various solutions/suggestions but haven't yet resolved this. So appreciate if someone can point what am I missing here.]

I have a custom UITabBarController with multiple tabs. Each is a UINavigationController. One of them ViewX is a custom UINavigationController.

I want the ViewX to not rotate to landscape (Only PortraitUp is supported). I was able to achieve that. However if I'm in the landscape mode on some other tab and tap on the tab with ViewX, I want it to rotate back to Portrait, but it stays in landscape.

The CustomUITabController has this code:

-(BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

The CustomUINavigationController has this implemented:

-(BOOL)shouldAutorotate
{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    return (interfaceOrientation != UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

I even tried this: This only rotates the Status bar to portrait but the view still remains in Landscape.

- (void)viewWillLayoutSubviews
{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if (interfaceOrientation != UIInterfaceOrientationPortrait)
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
}
Nithin Michael
  • 2,166
  • 11
  • 32
  • 56
parram
  • 309
  • 1
  • 2
  • 6

1 Answers1

0

I used following method to forcefully set portrait orientation only for one view and its work for me, may be it will be help you

1, Create one global flag

delegate.flagOrientationAll 

2, Add following code in your .m of the you view controller , for you project ViewX.m

// set flag "flagOrientationAll" to rotate only one view in your perticular view 
-(void)viewWillAppear:(BOOL)animated
{
    NSLog (@"webViewController -- viewWillAppear");
    [super viewWillAppear:animated];

    PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    NSLog (@"webViewController -- viewWillDisappear");
    PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = NO;

}

3. for other view you have to set

delegate.flagOrientationAll = NO;

4, Add method for Autorotation in you app delegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //NSLog(@"AppDelegate -- supportedInterfaceOrientationsForWindow");
    if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskPortrait;
    }else {
        return UIInterfaceOrientationMaskAll;
    } 
}  

Here my POST : How to set one of the screens in landscape mode in iphone?

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • Thanks for your inputs. Unfortunately it didn't work either. The solution is similar to what I had earlier but just at the appDelegate level. I still tried but it didn't work. The application:supportedInterfaceOrientationsForWindow: gets called and I see the value being returned is UIInterfaceOrientationMaskPortrait but that didn't help. The issue is if my device is in landscape mode, I'm in a different tab with the view also in landscape and now I switch to this tab, I want this tab with ViewX to rotate to Portrait. – parram Feb 24 '14 at 15:05