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

问题描述:

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

因此,我所做的是将状态栏方向更改为横向整理主页的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]也会返回纵向。我整天都在上网。并且实施许多由其他方案提供的解决方案,但我整天浪费了。有人可以指导我解决这些问题。

+0

你要在横向模式或某些屏幕的整个应用程序? – 2013-03-18 12:53:01

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(!)分类为UIApplication(!),但不要忘记这是真正需要的,或者我是为了方便(而不是将更改添加到50+视图控制器中)。但原则上上面是这样做的伎俩。

PS:你会找到一个更具有的相关详细代码样本一起回答这里: Presenting Navigation Controller in Landscape mode is not working ios 6.0

你只需要像: -

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

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


     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 

特定类要打开景观iOS6的只有

: -

-

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

    return UIInterfaceOrientationMaskLandscape; 
} 
+0

该方法在iOS 6中已弃用。 – omz 2013-03-18 12:54:31

+0

检查我的更新回答 – 2013-03-18 12:56:06

+0

原则上罚款。但它仍然需要用户实际旋转设备。 – 2013-03-18 13:09:14

你可以试试与

- (NSUInteger)supportedInterfaceOrientations { 
     return UIInterfaceOrientationMaskPortrait; 
    } 

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

或尝试呈现它作为一个模式,而不是推动它。