iOS:加载时的设备方向 [英] iOS: Device orientation on load

查看:104
本文介绍了iOS:加载时的设备方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎当我的应用加载时,它不知道它的当前方向:

It seems that when my app loads, it does not know its current orientation:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

一旦我旋转设备,我得到一个正确的方向,但当我加载应用程序时,不改变方向,似乎使用 [[UIDevice currentDevice] orientation] 不知道当前的方向。

Once I rotate the device, I get a correct orientation, but when I load the app, without changing the orientation, it seems that using [[UIDevice currentDevice] orientation] doesn't know the current orientation.

我第一次加载应用程序时是否还有另一种检查方式?

Is there another way to check this when I first load my app?

推荐答案

编辑:我误解了你的问题。这将允许您以某些方向启动应用程序。刚刚意识到你正试图弄清楚发布的方向。

I mis-read your question. This will allow you to start your application in certain orientations. Just realized you're trying to figure out the orientation on launch.

有一种方法可以检查 UIApplication上的状态栏方向

[[UIApplication sharedApplication] statusBarOrientation];






原始答案



尝试在plist文件中设置应用程序接受的设备方向:


Original answer

Try setting the application's accepted device orientations in the plist file:

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

这表示您的应用程序支持Portrait(底部的主页按钮),左侧横向和横向右边。

This will indicate that your application supports Portrait (home button at the bottom), landscape left, and landscape right.

然后,在你的UIViewControllers中,你需要覆盖 shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)方法来返回当应用程序应该旋转时是:

Then, in your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

如果设备位于您的某个设备中,这将告诉UIViewController自动旋转支持的方向。如果你也想支持倒置方向(顶部带有主页按钮的肖像),那么将它添加到你的plist中,然后从这个方法中返回YES。

This will tell the UIViewController to auto rotate if the device is in one of your supported orientations. If you wanted to support the upside-down orientation as well (portrait with home button on top) then add that to your plist and just return YES out of this method.

让我们知道它是如何运作的。

Let us know how it works out.

这篇关于iOS:加载时的设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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