iphone: Forcefully change orientation from portrai

2019-02-15 09:48发布

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.

3条回答
祖国的老花朵
2楼-- · 2019-02-15 10:19

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.

查看更多
Ridiculous、
3楼-- · 2019-02-15 10:28

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

查看更多
萌系小妹纸
4楼-- · 2019-02-15 10:30

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 :)

查看更多
登录 后发表回答