Question

I have an app that depending on the orientation shows different images... I have been using:

UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]);

To see if it is landsCape or not... but if I call this in my rootViewController: viewDidLoad,viewWillAppear, orviewDidAppear its always false.... then i was trying figure if its landsCape or no by comparing the width and height of a frame that has autosizeMask so it always is full screen... its a scroll view and I do the following:

BOOL landscape = self.frame.size.width>self.frame.size.height;

But that doesn't work either (always false) because the view still has the frame for the portrait view at that moment.

Was it helpful?

Solution

Instead of using device orientation, check for interface orientation. These macros will help you.

#define IS_PORTRAIT     UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
#define IS_LANDSCAPE    UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])

OTHER TIPS

You should do your layout in viewWillLayoutSubviews. At that point your view controller's view's frame will be set properly.

Using the orientation property is wrong. From the docs:

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface.

Instead, you may want to use [UIApplication sharedApplication].statusBarOrientation, which will match your UI's orientation in most cases.

What you can do is create a notification and then do your job accordingly!

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)     name:UIDeviceOrientationDidChangeNotification object:nil];

try using this aswell

[UIApplication sharedApplication].statusBarOrientation 

returns portrait when it's landscape, and landscape when it's portrait at launch, in iPad

I had an app that the launch orientation was critical for me to know. I was frustrated to learn that Apple's official position was that you should create your view in portrait. From the docs for UIViewController:

At launch time, apps should always set up their interface in a portrait orientation. After the application:didFinishLaunchingWithOptions: method returns, the app uses the view controller rotation mechanism described above to rotate the views to the appropriate orientation prior to showing the window.

You can follow Harsh's suggestion but it won't work…not quite how you need it to anyway. The reason why is that the correct orientation notification isn't generated for a second or so after the app has launched. I think Aaron's suggestion of using viewWillLayoutSubviews might be worth looking into.

On my app I referred to above, I didn't load the view until I got the first legitimate notification of what orientation the device was in. Instead I just showed the launch image for a split second longer until I knew which orientation to set the view up with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top