在横向启动后不能强制视图以纵向启动

after launch in landscape cannot force view to start in portrait

我知道之前有人问过这个问题,但我检查了所有解决方案,none 解决了我遇到的问题。

我的应用程序都是纵向格式,只有一个视图控制器是横向格式。

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

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

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

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

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

在应用委托中:

 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskPortrait;;
 }

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

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

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

这似乎是自 iOS11 以来的新事物。我通过允许自动旋转但仅支持纵向来修复它。

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

好的,这是我所做的,它最终运行良好,希望它可以帮助某人:

我在 appDelegate 中放置以下内容:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

为了介绍主视图,我使用了 appDelegate 中的以下内容:

self.navigationController = [[UINavigationControllerSub alloc] initWithRootViewController:libraryVC];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

portraitSize = CGSizeMake(navigationController.view.frame.size.width, navigationController.view.frame.size.height);

[self.window setRootViewController:navigationController]; 

在我从应用程序委托启动的主视图中,我需要它只是纵向的,我正在执行以下操作:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIApplication sharedApplication]     setStatusBarOrientation:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate setShouldRotate:NO];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}

当我想启动一个需要横向显示的视图时,我会在启动前使用以下命令:

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES];
[UIApplication sharedApplication].statusBarOrientation = UIDeviceOrientationLandscapeLeft;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];

您需要将您的项目设置为仅纵向,以确保无论设备的方向如何,应用程序都以纵向启动。

此外,请确保您已在您的 AppDelegate 上实施 supportedInterfaceOrientationsForWindow 并且它 returns UIInterfaceOrientationMaskAll 或任何对您的项目有意义的掩码。

有了这一切,您将让应用程序以纵向启动并控制每个视图控制器允许的方向,以及您已经拥有的实现。

我遇到了同样的问题。但我通过以编程方式旋转设备方向来修复它。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if UIDevice.current.orientation != .portrait {        
        UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")
    }

    return .portrait
}