为什么在iOS 8中我的应用程序在启动时采取错误的方向

24 cocoa-touch ipad uiinterfaceorientation ios

我的iOS 7应用程序正常工作.今天我用Xcode 6尝试了它,当我运行它时,我有一个令人讨厌的惊喜:(:

在此输入图像描述 您可以看到Xcode现在绘制我的viewController,就像它处于纵向模式一样,但正如您所看到的那样是横向模式.

你知道iOS 8中有什么变化吗?:/我使用了以下定位方法:

  • (NSUInteger)supportedInterfaceOrientations
  • (BOOL)shouldAutorotate
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

编辑:

我发现这个方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{
 return UIInterfaceOrientationMaskLandscape;
}
Run Code Online (Sandbox Code Playgroud)

现在我的第一个viewcontrollers正常工作:).问题是当我显示一个模态(改变UIInterfaceOrientationMaskAll中的方法集)时重新制作截图的丑陋效果.我正是在这种模式下改变我的方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.restrictRotation)
    {
        return UIInterfaceOrientationMaskLandscape;
    }    
    else
    {
        return UIInterfaceOrientationMaskAll;
    }
}


// THIS IS A DELEGATE OF MODAL VIEW CONTROLLER
- (void)updateInterfaceAfterDocumentPreviewViewController
{
    NSLog(@"updateInterfaceAfterDocumentPreviewViewController");

    [[UIApplication sharedApplication]     setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];

    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = YES;
}
Run Code Online (Sandbox Code Playgroud)

小智 16

请尝试以下代码

在... didFinishLaunchingWithOptionsAppDelegate

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

[self.window setFrame:[[UIScreen mainScreen] bounds]]; //Add
Run Code Online (Sandbox Code Playgroud)


小智 9

设置窗口时,问题似乎是调用的顺序.你需要调用makeKeyAndVisible您分配之前rootViewController在你didFinishLaunchingWithOptions的应用程序的委托方法.以下作品:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.myMainViewController;
Run Code Online (Sandbox Code Playgroud)

但是,如果您将订单更改为:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = self.myMainViewController;
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

你得到了你正在经历的行为.


mc0*_*c01 0

这种检测方向的方法在 iOS8 中已被弃用(但目前仍然允许)。它被 Size Classes 和Trait Collections的概念所取代。

(奇怪的是,我现在似乎无法在 Apple 的在线文档中找到 Size Class Reference - 发誓它以前就在那里 - 也许在 XCode 中)。请参阅此处查看 WWDC 视频。

不管怎样,Interface Builder 中有一个复选框,上面写着“ Use Size Classes ”。可能是你检查过了。将其关闭并看看是否有帮助?