0

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

所以我所做的就是在主页的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]返回纵向。我整天都在上网。并实施许多其他人提供的解决方案,但我的一整天都浪费了。有人可以指导我解决这些问题。

4

3 回答 3

1

你只需要喜欢:-

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

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


        return YES;
    }
    else
    {
        return NO;
    }
}

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

在 ios6 中:-

-

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

    return UIInterfaceOrientationMaskLandscape;
}
于 2013-03-18T12:53:13.037 回答
0

你可以试试

- (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskPortrait;
  }

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

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

于 2013-03-18T12:56:16.713 回答
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

于 2013-03-18T13:05:32.307 回答