用户在纵向视图中强制加载横向视图

时间:2013-03-18 12:49:42

标签: iphone objective-c uinavigationcontroller device-orientation

在我的应用程序中,我使用导航控制器来管理视图。我的登录页面支持纵向和横向视图。当用户登录我的第二个视图是家庭并且它仅支持横向模式。我想要做的是当用户使用纵向视图主页登录到家时,即使设备是纵向的,也应该以横向视图显示。

所以我做的是将状态栏方向更改为主页的viewWillAppear方法中的横向,如下所示;

    -(void)viewWillAppear:(BOOL)animated{

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

我也覆盖了shouldAutorotateToInterfaceOrientation如下

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return YES;

}

我的问题是,即使状态栏已更改为横向,我的UIViewController(主页)仍处于横向模式。当我调试时,我发现即使我将状态栏方向更改为横向,[[UIDevice currentDevice] orientation]也会返回纵向。我整天都在上网。并实施了许多其他解决方案,但我整天浪费了。有人可以指导我解决这些问题。

3 个答案:

答案 0 :(得分:1)

你只需要: -

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

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


        return YES;
    }
    else
    {
        return NO;
    }
}

对于您要打开横向的特定课程

在ios6中

: -

-

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

    return UIInterfaceOrientationMaskLandscape;
}

答案 1 :(得分:0)

您可以尝试使用

- (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskPortrait;
  }

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

或尝试将其作为模态呈现,而不是推动它。

答案 2 :(得分:0)

Apple不希望您强制设备的方向。但是有一个技巧。 不幸的是我没有访问我的代码。

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. 

没关系。但它仍然需要用户实际旋转设备。否则,将不会调用整个方向更改机制。 现在,当您想强制更改方向时,请执行以下操作:

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. 

在回来的路上反之亦然:)

使用标签栏时,以上所有内容都会变得更加复杂。你用标签栏吗? 我设法使用一个标签栏,我必须子类来覆盖它的旋转方法。在没有标签栏的应用程序中,我将UIApplication(!)子类化了,但是没有真正需要的或者更好地为了方便而做到这一点(而不是将更改应用于50多个视图控制器)。但原则上,上述是诀窍。

PS:您可以在此处找到更详细的答案以及代码示例: Presenting Navigation Controller in Landscape mode is not working ios 6.0