【问题标题】:Force portrait in one view controller makes other to be in portrait initially在一个视图控制器中强制纵向使其他视图控制器最初处于纵向
【发布时间】:2013-05-18 15:13:13
【问题描述】:

导航控制器的根视图控制器仅支持纵向,其他控制器支持所有方向。现在,如果我在根视图控制器上并且设备处于横向并且如果我推动下一个以纵向打开的视图控制器应该打开在横向,因为它支持所有方向。

请帮我解决这个问题。

使用 iPhone 4s iOS6.1.3

【问题讨论】:

  • 但是如果您继续旋转设备,它会旋转吗?还是保留在纵向上?
  • @jcesar:如果我旋转设备,它会旋转。

标签: iphone ios cocoa-touch uiinterfaceorientation device-orientation


【解决方案1】:

您可以在登录视图控制器后使用以下代码在您的第一个屏幕中检查设备方向:-

-(void)viewWillAppear:(BOOL)animated
    {
        [self willRotateToOrientation:[[UIDevice currentDevice] orientation]];  
        [super viewWillAppear:YES];
    }


- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
        if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        {
            if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {

              //set your landscap View Frame
                [self supportedInterfaceOrientations];

            }



        }
        else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
            if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
      //set your Potrait View Frame
                [self supportedInterfaceOrientations];

            }
        }
        // Handle rotation
    }

当您加载此视图控制器时,它会首先检查设备方向,然后加载它的相关帧

【讨论】:

  • 感谢您的回答,我正在做同样的事情,但没有帮助。 :( 无论如何,我赞成您的回答,因为它可能对其他人有所帮助。
【解决方案2】:

我认为这是与 iOS6 中的方向变化有关的问题。你需要继承UINavigationController

查看this

【讨论】:

    【解决方案3】:

    1 . 你必须创建 UINavigationController 的子类。添加Following方法..取一个布尔变量检查它是否支持所有方向并更改其值。

     @implementation NavigationControllerViewController
    
    - (BOOL)shouldAutorotate
    {
    return YES;
    }      
    
    - (NSUInteger)supportedInterfaceOrientations
    {
    AppDelegate *appdelgate=[[UIApplication sharedApplication]delegate];
    
    if (appdelgate.issuppoertAll) {
        // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
        return UIInterfaceOrientationMaskAll;
    
    }
    return UIInterfaceOrientationMaskPortrait;
    
      }
     @end
    

    2当您将表单根视图控制器导航到其他视图控制器时

    当你想强行改变它的方向时,使用这个代码。即横向到纵向

          obj_viewcontroller        = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        [self presentModalViewController:obj_viewcontroller animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    
        [self.navigationController pushViewController:obj_viewcontroller animated:NO];
    

    3 在第二个视图控制器中,您必须更改布尔变量值

    -(void)viewWillAppear:(BOOL)animated
    {
    appdelgate.issuppoertAll=YES;
      }
    

    4将此方法添加到所有视图控制器中,并根据需要设置方向。

    - (NSInteger)supportedInterfaceOrientations 
    {
    return UIInterfaceOrientationMaskPortrait;
     }
    

    【讨论】:

    • 我正在尝试这个,看起来很有希望
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多