xcode 使用 statusBarOrientation 的 iPad 设备方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24312414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:06:53  来源:igfitidea点击:

iPad Device orientation using statusBarOrientation

iosxcodeipadorientationswift

提问by kmiklas

(Xcode 6, Swift, iOS8)

(Xcode 6、Swift、iOS8)

I am trying to find the device orientation for iPad, and it seems to be needlessly complicated. where am I going wrong?

我试图找到 iPad 的设备方向,但它似乎不必要地复杂化。我哪里错了?

At first, I tried to use the interfaceOrientationof my UIViewController, but it is unreliable and deprecated. Apple recommend using the statusBarOrientation property. (1)

起初,我尝试使用interfaceOrientationUIViewController 的 ,但它不可靠且已弃用。Apple 建议使用 statusBarOrientation 属性。(1)

statusBarOrientation is of type UIApplication. I tried to create an instance like so:

statusBarOrientation 的类型为UIApplication。我试图创建一个这样的实例:

var o: UIApplication

and then test:

然后测试:

if (o.statusBarOrientation.isLandscape) { ... }

but received error

但收到错误

Variable 'o' used before being initialized.

Which makes no sense to me at all. Why would I initialize it to a value? I would overwrite the value that I want!?

这对我来说毫无意义。为什么我要把它初始化为一个值?我会覆盖我想要的值!?

Then I tried simply creating a variable as recommended in the docs:

然后我尝试按照文档中的建议简单地创建一个变量:

var statusBarOrientation: UIInterfaceOrientation

but on a trace:

但有迹可循:

statusBarOrientation= (UIKit.UIInterfaceOrientation) (0xa0)

statusBarOrientation= (UIKit.UIInterfaceOrientation) (0xa0)

So I tried to subclass UIApplication, and access the property through my subclass.

所以我尝试将 UIApplication 子类化,并通过我的子类访问该属性。

From here, more complexity. It appears that "Every app must have exactly one instance of UIApplication (or a subclass of UIApplication)." (2)

从这里开始,更加复杂。看起来“每个应用程序都必须只有一个 UIApplication 实例(或 UIApplication 的子类)。” (2)

This led me to the following post, which seems to create a Mainroutine in Swift?!
Subclass UIApplication with Swift

这让我看到了以下帖子,它似乎Main在 Swift 中创建了一个例程?!
使用 Swift 继承 UIApplication

Again, my goal is to grab the current device orientation, and test it in a conditional. Pseudocoded:

同样,我的目标是获取当前设备方向,并在条件下对其进行测试。伪代码:

var o = (get device orientation)
if (o == portrait ) { ... } else { ... }

As you can see, I'm in the weeds... any help would be greatly appreciated.

正如你所看到的,我在杂草丛中......任何帮助将不胜感激。

EDIT: I did manage to get it sort-of working with the following:

编辑:我确实设法让它在以下方面工作:

var o = UIApplication.sharedApplication().statusBarOrientation;
if (o.isLandscape) { ...

however, on initial load, when the device is rotated to landscape, o.isLandscapeis being reported as false.

但是,在初始加载时,当设备旋转到横向时,o.isLandscape报告为false

  1. Search documentation for "UIViewController," and look in the "Configuring the View Rotation Settings." Under Discussion, "Do not use this property for informing layout decisions. Instead, use the statusBarOrientationproperty, described in UIApplication Class Reference."

  2. Search documentation for "UIApplication" and look under Subclassing notes

  1. 搜索“UIViewController”文档,并查看“配置视图旋转设置”。在Discussion,“不要使用此属性通知布局决策。而是使用UIApplication 类参考中statusBarOrientation描述的属性

  2. 搜索“UIApplication”的文档并查看子类注释

回答by Mick MacCallum

There appear to be a couple of things wrong here that I'll try to clear up. First of all, the following doesn't create an instance of UIApplication, merely a variable of that type:

这里似乎有几件事不对,我会尽力澄清。首先,以下不会创建 UIApplication 的实例,只是该类型的变量:

var o: UIApplication

If you want to create an instance of UIApplication, you should be doing this:

如果你想创建一个 UIApplication 的实例,你应该这样做:

let o: UIApplication = UIApplication()

But, as you discovered, this won't work for UIApplication; you shouldn't be creating additional instances of UIApplication. Instead, you should be accessing the singleton instance, sharedApplication():

但是,正如您所发现的,这不适用于UIApplication; 您不应该创建 UIApplication 的其他实例。相反,您应该访问单例实例sharedApplication()

let o: UIApplication = UIApplication.sharedApplication()

Note:You had the same issue with your orientation variable as you did with the app delegate variable. The following line doesn't say what valueto put into variable statusBarOrientation; it is not initialized:

注意:您的方向变量与应用程序委托变量存在相同的问题。以下行没有说明将什么值放入变量statusBarOrientation;它没有被初始化:

var statusBarOrientation: UIInterfaceOrientation

Any oneof these lines does what you intended:

这些行中的任何一行都符合您的意图:

var statusBarOrientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
var o: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
let orientation = UIApplication.sharedApplication().statusBarOrientation

Anyway, when you put it all together, you can get the current status bar orientation with the following.

无论如何,当你把它们放在一起时,你可以通过以下方式获得当前的状态栏方向。

if let theAppDelegate = UIApplication.sharedApplication() {
    let orientation = theAppDelegate.statusBarOrientation

    if orientation.isPortrait {
        // Portrait
    } else {
        // Landscape
    }
}


ALSO

You need to specify that your app can support the landscape orientations. This can either be done via the orientation check boxes in the application summary, or programmatically through the view controller's supported orientations method (you can find plenty on this online).

您需要指定您的应用程序可以支持横向。这可以通过应用程序摘要中的方向复选框来完成,也可以通过视图控制器支持的方向方法以编程方式完成(您可以在网上找到很多)。

回答by amurray4

This works with Xcode 6.1 to avoid the "Bound value in a conditional binding must be of Optional type UIApplication" error that occurs with the use of an if let statement.

这适用于 Xcode 6.1,以避免在使用 if let 语句时出现“条件绑定中的绑定值必须是可选类型 UIApplication”错误。

let orientation = UIApplication.sharedApplication().statusBarOrientation

    if orientation.isPortrait {
        // Portrait
    } else {
        // Landscape
    }