iOS:加载时的设备方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5888016/
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-08-30 19:48:55  来源:igfitidea点击:

iOS: Device orientation on load

iphoneiosuiinterfaceorientationuidevice

提问by Nic Hubbard

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
}

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.

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

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

当我第一次加载我的应用程序时,还有另一种方法来检查这个吗?

回答by groomsy

EDIT: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.

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

There is a method to check the status bar orientation on UIApplication:

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

[[UIApplication sharedApplication] statusBarOrientation];


Original answer

原答案

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

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

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

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

这将表明您的应用程序支持纵向(底部的主页按钮)、横向左侧和右侧横向。

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

然后,在您的 UIViewControllers 中,您将需要重写该shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)方法以在应用程序旋转时返回 YES:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

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

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.

如果设备处于您支持的方向之一,这将告诉 UIViewController 自动旋转。如果您也想支持倒置方向(顶部带有主页按钮的纵向),请将其添加到您的 plist 中,然后从该方法中返回 YES。

Let us know how it works out.

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

回答by Iducool

I think this will work:

我认为这会奏效:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

According to the UIDevice reference:
Quote:
"The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications"
I had initially assumed that this property contained the current orientation at all times, but not so, apparently. I guess that turning on notifications is being handled for us behind the scenes in other situations where the orientation property is typically accessed, so it wasn't obvious that this needs to be done manually inside the app delegate

根据 UIDevice 参考:
引用:
“除非通过调用 beginGeneratingDeviceOrientationNotifications 启用方向通知,否则此属性的值始终返回 0”
我最初假设此属性始终包含当前方向,但显然并非如此。我猜在通常访问方向属性的其他情况下,我们会在幕后为我们处理打开通知,因此这并不明显需要在应用程序委托中手动完成

回答by Marijn

On load the device orientation can be .Unknownor .FaceUp. To figure out whether it's portrait or landscape I use the statusBarOrientationas a back-up, like this:

加载时,设备方向可以是.Unknown.FaceUp。为了弄清楚它是纵向还是横向,我将其statusBarOrientation用作备份,如下所示:

    var portraitOrientation = UIDevice.currentDevice().orientation == .Portrait

    if UIDevice.currentDevice().orientation == .Unknown || UIDevice.currentDevice().orientation == .FaceUp {
        portraitOrientation = UIApplication.sharedApplication().statusBarOrientation == .Portrait
    }

This way I can assure that portraitOrientationalways tells me if the device is in portrait mode, and if not it will be in landscape. Even on loading the app for the first time.

通过这种方式,我可以确保portraitOrientation始终告诉我设备是否处于纵向模式,如果不是,它将处于横向模式。即使是第一次加载应用程序。

回答by Ahmed Hammad

You can do it by inserting the following notification inside

您可以通过在其中插入以下通知来完成

-(void)viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

then put the following method inside your class

然后将以下方法放入您的类中

-(void)checkRotation:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
         //Do your textField animation here
    }
}

The above method will check the orientation of the status bar of the ipad or iPhone and according to it you make do your animation in the required orientation.

上述方法将检查 ipad 或 iPhone 状态栏的方向,并根据它以所需的方向制作动画。

回答by Jeff Kelley

One thing that nobody has touched on yet is that you're storing UIDeviceOrientationtypes in a UIInterfaceOrientationvariable. They are different, and should not be treated as equal. Note that UIDeviceOrientationLeftis equal to UIInterfaceOrientationRight(since the interface rotates the opposite way compared to the device).

尚未有人触及的一件事是您将UIDeviceOrientation类型存储在UIInterfaceOrientation变量中。它们是不同的,不应被平等对待。请注意,UIDeviceOrientationLeft它等于UIInterfaceOrientationRight(因为与设备相比,界面以相反的方式旋转)。

回答by Nacho Badia

Swift 3based on @Marjin 's code.

Swift 3基于 @Marjin 的代码。

var portraitOrientation = UIDevice.current.orientation == .portrait

if UIDevice.current.orientation == .unknown || UIDevice.current.orientation == .faceUp {
     portraitOrientation = UIApplication.shared.statusBarOrientation == .portrait
}

if(portraitOrientation)
{
     // Portrait
}
else
{

}

回答by Bilal ?im?ek

for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.

对于那些寻找 Swift 3 或 4 答案的人。只需将该代码添加到 viewDidLoad() 块中。

let orientation = UIApplication.shared.statusBarOrientation
if orientation == .portrait {
      // portrait   
} else if orientation == .landscapeRight || orientation == .landscapeLeft{
      // landscape     
}

update for depreciation alerts for IOS 13 and Swift 5.x use code block below.

IOS 13 和 Swift 5.x 的折旧警报更新使用下面的代码块。

 if #available(iOS 13.0, *) {
        let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
        if orientation == .portrait {
                     // portrait

               } else if orientation == .landscapeRight || orientation == .landscapeLeft{
                     // landscape
               }
    } else {
        // Fallback on earlier versions
        let orientation = UIApplication.shared.statusBarOrientation
        if orientation == .portrait {
                            // portrait

    } else if orientation == .landscapeRight || orientation == .landscapeLeft{
                               // landscape
    }
    }

回答by NicoRaf

In order to obtain the orientation from the status bar it is also important to have the all the orientations enabled at the plist file.

为了从状态栏获取方向,在 plist 文件中启用所有方向也很重要。

回答by jenish

Try this one. it's work for me. gnarly at that time of didfinishedlaunch method not detect device orientation. its take by default as a portrait. so. i am using to check stats bar orientation . i test this code. put it in didfinishedlaunch method in appdeleget.

试试这个。这对我有用。当时的 didfinishedlaunch 方法很粗糙,没有检测到设备方向。它默认为肖像。所以。我用来检查统计栏方向。我测试这段代码。把它放在 appdeleget 中的 didfinishedlaunch 方法中。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

UIInterfaceOrientation 方向 = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) {//Default orientation
    //UI is in Default (Portrait) -- this is really a just a failsafe.

    NSLog("for portrait");


}else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{

    NSLog("portrait");
}else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{

    NSLog("Landscap");
}

回答by Sabby

Try This one [[UIApplication sharedApplication] statusBarOrientation];

试试这个 [[UIApplication sharedApplication] statusBarOrientation];

or implement this one in app delegate

或在应用程序委托中实现这个

(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
}

it works

有用