在横向启动后不能强制以纵向启动视图 [英] after launch in landscape cannot force view to start in portrait

查看:110
本文介绍了在横向启动后不能强制以纵向启动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前曾有人问过这个问题,但是我仔细研究了所有解决方案,但都没有解决我遇到的情况.

I know that this subject has been asked before, but I went over all solutions and none solved the case I'm having.

我的应用全部为纵向格式,只有一个视图控制器为横向格式.

My app is all in portrait format, only one view controller in landscape format.

我向所有视图控制器添加了以下代码,无论设备的方向如何,都强制执行纵向方向":

I added to all view controllers the following code to force Portrait Orientation regardless the device orientation:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       return UIInterfaceOrientationMaskPortrait;
 }

 -(BOOL)shouldAutorotate{
      return NO;
 }
 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskPortrait;
 }

 -(void)ViewDidLoad()
 {
      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
 }

如果将设备设置为纵向模式,则viewController可以正确打开,但是如果在打开应用程序之前将设备设置为横向模式,则应用程序将以横向模式启动启动屏幕,并以部分视图的横向模式打开视图控制器剪裁,然后将其旋转到纵向模式,并保持部分屏幕剪裁,如图所示.

If the device is set in portrait mode the viewController opens correctly, but if the device is set landscape mode before opening the app, then the app launches the splash screen in landscape and opens the view controller in landscape with part of the view clipped, then it rotates it to portrait mode, and keeps part of the screen clipped, as shown in the picture.

在应用程序委托中:

 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskPortrait;;
 }

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
 {
      return UIInterfaceOrientationMaskAllButUpsideDown;
 }

在appDelegate中,在将视图控制器添加到导航控制器之前,我添加了以下内容:

And in the appDelegate also, just before adding the view controller to the navigation controller I added the following:

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];

推荐答案

自iOS 11以来,这似乎是新事物.我通过允许自动旋转但仅支持人像的方式对其进行了修复.

This seems to be a new thing since iOS 11. I fixed it by allowing autorotate but only supporting portrait.

(BOOL)shouldAutorotate{
      return YES;
}
(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskPortrait;
}

这篇关于在横向启动后不能强制以纵向启动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆