Question

In my Application I'm using a navigation controller to mange views. My login page support both portrait and landscape views. When user logged in my second view is home and it support only landscape mode. What I want to do is when user login to the home using portrait view home page should appear in landscape view even though the device in portrait.

So what I did was I change the status bar orientation in to landscape int the home page's viewWillAppear method as follows;

    -(void)viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO];
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation];
    }

also I have override the shouldAutorotateToInterfaceOrientation as follows

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return YES;

}

My problem is even the status bar changed to landscape my UIViewController (home) is remains in landscape mode. When i'm debugging I found that even I change the status bar orientation to landscape,[[UIDevice currentDevice] orientation] returns portrait. I went through internet whole day. And implement lot of solutions proviede by other but my whole day wasted. can some one guide me to solve these issue.

Was it helpful?

Solution 2

Apple does not want you to force the orientation of the device. There is a trick though. Unfortunately I do not have access to my code.

1. Your app in general supports all orientations. 
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations). 
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations. 

That is fine. But it would still require the user to actually rotate the device. Otherwise the whole orientation change mechanism would not be invoked. Now, when you want to force the orientation change, do the following:

4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed. 
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally. 
5a. So if you want to present the rotated view controller modally, then do it. 
5b. If you still need to push it then: 
5b1. Create an empty UIViewController instance. alloc/init will do. 
5b2. Present it modally
5b3. Dismiss it modally 
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.  
5c4. Next push the view controller that you want to display roated. 

And vice versa on your way back :)

All the above gets more complicated when you use a tab bar. Do you use a tab bar? I managed to get that working with a tab bar which I had to subclass to overwrite its rotation methods. In an app without tab bar I subclassed UIApplication (!) but don't rembember wether that was really required or wether I did that out of convenience (instead of aplying the changes to 50+ view controllers). But in principle the above is it that does the trick.

PS: You find a more detailled answer here along with code samples: Presenting Navigation Controller in Landscape mode is not working ios 6.0

OTHER TIPS

you just need to like:-

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {


        return YES;
    }
    else
    {
        return NO;
    }
}

for particular class you want to open landscape Only

in ios6:-

-

(BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscape;
}

You can try with

- (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskPortrait;
  }

and

- (BOOL)shouldAutorotateToInterfaceOrientation:     (UIInterfaceOrientation)toInterfaceOrientation {
     return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
 }

or try presenting it as a Modal, rather than pushing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top