iphone: Forcefully change orientation from portrait to landscape on navigation

醉酒当歌 提交于 2019-12-03 21:35:49

问题


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.



来源:https://stackoverflow.com/questions/11610819/iphone-forcefully-change-orientation-from-portrait-to-landscape-on-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!