简体   繁体   中英

How to get current orientation of device programmatically?

I want to know the device orientation, I used

 UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation

but this only works when I change the orientation otherwise it does not give anything (TRUE/FALSE).So how can I get current device orientation even when orientation is not changed.

Thanks in advance!

To get orientation

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

and the possible orientations are

typedef enum {
  UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeLeft,
  UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

Gives an Implicit Enumeration Warning.

Use this instead:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Sorry if you've already done this, but have you looked into the beginGeneratingDeviceOrientationNotifications method, which enables notification generation for the device?

Also, are you looking for the device orientation or the interface orientation? If you're looking for a simpel Portrait vs Landscape orientation info, you should look into the interfaceOrientation of the view controller.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM