Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get device orientation via UIView reference?

I need to get device orientation from a ViewController. I cannot base on:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

because it sometimes returns orientation unknown (for example when the device lays on the table).

I just need to know in what orientation is my current UIView displayed (is it landscape left or landscape right). I don't need to have this value updated when orientation changes, I just want to know it, when I ask for it. Just some reference view.orientation ;). Is there something that would help me? I've read UIView documentation, found some reference to UIWindow, but nothing that could help me.

like image 760
thelion Avatar asked Apr 26 '12 12:04

thelion


2 Answers

You can also get the device orientation from UIApplication, have you tried using

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
like image 170
vishy Avatar answered Nov 18 '22 15:11

vishy


UIViewController has a property

UIInterfaceOrientation interfaceOrientation;

So in your UIViewController, you can access the current orientation of the device at any time via

UIInterfaceOrientation myCurrentOrientation = self.interfaceOrientation;
like image 25
steveb Avatar answered Nov 18 '22 17:11

steveb