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


春风助手
2025-03-09 05:17:23 (13天前)

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

因此,我要做的就是viewWillAppear按以下方式将状态栏的方向更改为横向于首页的方法。


  1. -(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]返回纵向。我整天都上网。并实施其他许多解决方案却被我浪费了整整一天。有人可以指导我解决这些问题。

2 条回复
  1. 1# 只怕再见是故人 | 2020-08-17 09-36

    Apple不想让您强行调整设备的方向。虽然有一个窍门。不幸的是,我无权访问我的代码。

    1. 1. Your app in general supports all orientations.
    2. 2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations).
    3. 3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations.

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

    1. 4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed.
    2. 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.
    3. 5a. So if you want to present the rotated view controller modally, then do it.
    4. 5b. If you still need to push it then:
    5. 5b1. Create an empty UIViewController instance. alloc/init will do.
    6. 5b2. Present it modally
    7. 5b3. Dismiss it modally
    8. Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.
    9. 5c4. Next push the view controller that you want to display roated.

    反之亦然:

    使用选项卡栏时,以上所有内容都会变得更加复杂。您是否使用标签栏?我设法使用选项卡栏来实现这一点,而该选项卡栏必须子类化以覆盖其旋转方法。在没有选项卡栏的应用程序中,我将UIApplication(!)子类化,但不要忘记确实需要的东西,还是出于方便考虑(而不是将更改应用于50+个视图控制器)。但原则上,以上是解决问题的办法。

    PS:您可以在此处找到更详细的答案以及代码示例: 在“横向”模式下显示“导航控制器”无法正常运行ios 6.0 https://stackoverflow.com/questions/15519486/presenting-navigation-controller-in-landscape-mode-is-not-working-ios-6-0/15528854#15528854

登录 后才能参与评论