iphone: Forcefully change orientation from portrai

2019-02-15 10:13发布

问题:

Is there any way that we can Forcefully change app orientation from portrait to landscape while navigating ?

I have a requirement that while pushing controller from A to B. B should be in landscape but my A controller is in portrait.

回答1:

In your ViewController-B add this lines of code in viewDidLoad:

  [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

  float   angle = M_PI/2;  //rotate 180°, or 1 π radians
  self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

This code is working fine if you are using navigation controller to move from A to B.

I have used this.

Hope this will help you.

Enjoy Coding :)



回答2:

In - (void)viewWillAppear:(BOOL)animated say:

    //this is to force the application to re-evaluate device orientation
    //comment the last lines and see
    //cannot use [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
    //as "setOrientation" is a private method, and we risk to be kicked out by Apple
    UIViewController *c = [[[UIViewController alloc]init] autorelease];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];

... but first, in your xib, set your view's orientation to the desired one



回答3:

from my experience on the last app in which I tried doing the same, I found answers on Stack overflow telling not to forcibly change the orientation in a Navigation based application.

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
else
    return YES;
}

this function will let you use the view controller in the desired orientation, in this case I've set it for landscape, and disallowed rotation in portrait modes.

You will face the problem of the B view controller loading first in the orientation on A view controller thou. So I changed the full orientation of the app to landscape. Hope this helps.