iOS8中设备旋转后的ViewController偏移量 [英] ViewController offset after device rotation in iOS8

查看:137
本文介绍了iOS8中设备旋转后的ViewController偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了在iOS8中运行的遗留代码的奇怪问题。该代码最初是在iOS6之前编写的,故事前预告板(使用笔尖)并且在iOS7上没有问题。但是,在iOS8上运行时开始出现奇怪的行为。

I am experiencing an odd problem with legacy code running in iOS8. The code was originally written pre-iOS6, pre-storyboards (using nibs) and functioned without issue on iOS7. However, strange behavior began to occur when running on iOS8.

应用程序设置为仅在横向(左或右)中运行。因此它应该支持从横向左到横向右旋的自动旋转。第一个问题是初始视图控制器是纵向加载的,一侧被切断。此问题已通过AppDelegate中的以下代码解决:

The app is set to run only in landscape (left or right). Therefore it should support autoRotation from landscape-left to landscape-right. The first problem was that the initial view controller was loaded in portrait and one side was cut off. This issue was addressed with the following code in the AppDelegate:

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.window setFrame:[[UIScreen mainScreen] bounds]];

更改这些调用的顺序允许视图控制器以正确的方向加载并且不会影响应用程序在iOS7中运行。但是,现在当设备旋转180度时,我得到以下结果。查看图片...

Changing the order of these calls allowed the view controller to load in its proper orientation and did not affect apps running in iOS7. However, now when the device is rotated 180 degrees I get the following results. See images...

以下是初始加载时的应用:

Here is the app at initial load:

旋转180度后,我得到这种偏移效果:

And after a 180 degree rotation, I get this offset effect:

有关如何解决此问题的任何想法?同样,iOS7或之前的一切都很好。谢谢!

Any ideas on how to address this issue? Again, everything is fine in iOS7 or previous. THanks!

推荐答案

当iOS 8首次出现时我和你的一样有问题,而且与开启窗口无关 - 创作咒语!它与您的 Info.plist 有关。 Info.plist 中列出的 first 方向必须是根视图控制器实际允许的方向。

I had a problem just like yours when iOS 8 first appeared, and it had nothing to do with the opening window-creation incantation! It has to do with your Info.plist. It is crucial that the first orientation listed in the Info.plist be an orientation that the root view controller actually permits.

因此,假设您的根视图控制器仅允许横向显示。如果您的Info.plist如下所示,那么得到您描述的问题:

So, let's say your root view controller permits only landscape. Then you will get the problem you describe if your Info.plist looks like this:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

查看错误?肖像是第一位的。 没有。如果根视图控制器仅允许横向,则其中一个横向方向必须首先出现。

See the bug? Portrait comes first. No. If the root view controller permits only landscape, then one of the landscape orientations must come first.

[您实际上可以看到我遇到此问题,然后在此处发现并报告解决方案: https://stackoverflow.com/a/24467576/341994]

[You can actually see me experiencing the issue, then discovering and reporting the solution, here: https://stackoverflow.com/a/24467576/341994]

另外,我不得不怀疑你是否会做任何扰乱旋转系统的 else 。请注意,旋转本身的性质,包括坐标系,旋转时,旋转时等,在iOS 8中完全改变;这可能是iOS 8中最大的单一变化。因此,如果你有一些旧的代码可以对旋转的内容进行底层假设(例如它涉及转换),那么该代码现在将会中断。你没有在你的问题中显示任何内容,所以我不能提供你可能在做什么的细节。

Also, I have to wonder whether you might be doing anything else that upsets the rotation system. Note that the nature of rotation itself, including the coordinate system, what rotates, when it rotates, etc., has completely changed in iOS 8; this is probably the biggest single change in iOS 8. So if you have old code that makes under-the-hood assumptions about what rotation is (e.g. that it involves a transform), that code will now break. You do not show any of that in your question so I can't provide specifics about what you might be doing.

最后,只是为了记录,正确的最小开口咒语是(这是Swift代码,但我相信你可以翻译):

Finally, just for the record, the correct minimal opening incantation is (this is Swift code but I'm sure you can translate):

func application(application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window!.rootViewController = UIViewController() // or whatever it is
    self.window!.backgroundColor = UIColor.whiteColor()
    self.window!.makeKeyAndVisible()
    return true
}

这篇关于iOS8中设备旋转后的ViewController偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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