How to get statusBarOrientation on first view - ios

How does one detect the correct orientation of the application on the first view? I need to know so I can either display a portrait image or landscape image as the background.
[[UIDevice currentDevice] orientation] doesn't work properly if the iPad is laid down flat on a surface.
statusBarOrientation doesn't seem to return the correct results for the very first view that is loaded. I tried getting the status bar orientation in the viewWillAppear and viewDidLoad and both returned Portrait even when in landscape.
I have confirmed that I have [self.window makeKeyAndVisible] in my AppDelegate.m in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. This is only returning the incorrect status bar orientation on the first view controller.
Note: I have also tried the interfaceOrientation from the UIViewController, ad that still returns portrait no matter what.
Any ideas how to fix this?
EDIT:
I am using the [[UIDevice currentDevice] orientation] to detect if it is portrait or landscape but my issue is detecting what the view actually is if UIDevice returns an orientation of flat (non portrait or landscape).

Many applications have this issue. If the device is flat then any of the orientations is valid.
Your best bet is to use the [[UIDevice currentDevice] orientation] to know what the orientation is if possible for the cases where the orientation can be determined. But when it's flat, it'll just be whatever it was last.
How many times have you see people flick up their iPad to get the landscape orienation the right way around.

GOT IT!
I was able to do a work around by using
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Even on view load this method is triggered if the orientation is not of UIInterfaceOrientationPortrait.
Even if the device is flat, it seems that there is a long enough delay for the orientation to correct itself and detect whether the UIView is in portrait or landscape mode.
EDIT:
When viewDidAppear is triggered by then, [self interfaceOrientation] has the correct value. So if you can wait until that method, you can also get it there.

Related

Landscape Mode in iOS 8+ not displaying correctly

I have a couple of older Cocos2d games that, when updating, I noticed appear truncated upon launch. Having found other answers here on StackOverflow I am still having issues.
The workaround I currently use is;
Inside AppDelegate.m I am using
CGAffineTransform transform = CGAffineTransformMakeRotation(1.57079633);
navController_.view.transform = transform;
And setting the device orientation to Portrait & Portraitupsidedown - even though the game is Landscape.
This is a dirty fix, and brings it's own set of issues, such as UIAlert views not appearing at the correct orientation, as well as the screen not rotating landscape upside down (unless you hold it portraitupsidedown) which gets confusing, and pretty sure it is against Apple's review rules and just make the whole workaround useless really.
Effectively I need to trick it so that I can enable Landscape mode rather than Portrait, yet rotate the screen on launch, has anyone managed this successfully?
Add this in AppDelegate.m class
(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscape;
}
Project file -> 'Targets' -> Choose you orientation to Landscape

A UIInterfaceOrientation issue

I want my iPhone app to globally behave as if only the UIInterfaceOrientationPortrait was permitted.
But at the same time I want it to be aware of the physical orientation in order to react in a specific way, only within a limited area of the display.
How can I get this?
I did some tests using the self.interfaceOrientation iVar of the UIViewController, but that does not seem to work, because this variable does not change.
First, do [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]. Then register for UIDeviceOrientationDidChangeNotification. When you receive the notification, check [UIDevice currentDevice].orientation for the device's physical orientation.
You should definitely read the documentation for these APIs, because it contains some important warnings.
Also note that device orientation is returned as a UIDeviceOrientation, which is different than a UIInterfaceOrientation. Keep in mind this information from the UIInterfaceOrientation documentation:
You use these constants in the statusBarOrientation property and the setStatusBarOrientation:animated: method. Notice that UIDeviceOrientationLandscapeRight is assigned to UIInterfaceOrientationLandscapeLeft and UIDeviceOrientationLandscapeLeft is assigned to UIInterfaceOrientationLandscapeRight; the reason for this is that rotating the device requires rotating the content in the opposite direction.

Get iPad Landscape Orientation in iOS5 at startup

I've got an iPad App with a TabBarController. Since iOS5 I'm note able anymore to get the correct orientation in the viedDidLoad or viewWillAppear when I start the app on the iPad in landscape. It always returns Portrait. When I ask for the orientation in viewDidAppear, everything is fine and correct. In iOS4 I was able to get the right orientation, but now I build for iOS5 ... not anymore.
Please. What can I do to get the right orientation on the iPad on startup?
This is how I get my orientation:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
Thanks!
If you are loading any viewcontroller before loading RootViewContoller then Orientation of RootViewController needs to be set before adding it to window. you may find answer HERE

Determining the Orientation of the screen rather than the orientation of the device

The [[UIDevice currentDevice] orientation] method returns a number of orientations beyond the portrait and landscape orientation. I am well aware of checking to see if the orientation returned is "valid", unfortunately if the orientation returned is NOT "valid" when my app requires a specific orientation to determine which methods to run, I have no way of knowing which method is appropriate.
I have attempted a number of solutions to this, but for now the only thing I have been able to work out is to check to see if the orientation is LandscapeLeft or LandscapeRight and if it isn't I assume the screen is in portrait orientation. Unfortunately this isn't true when the device is face up and the screen is in landscape orientation.
I attempted to use the parent view controller's orientation:
parentController.interfaceOrientation;
Unfortunately it returned UIDeviceOrientationUnknown. I search SO and Google for someone else who has faced this problem. It seems silly to me that Apple would have anything but LandscapeLeft/Right and PortraitUp/Down for their orientations.
What I really need is the APPS orientation not the devices, since these are sometimes at odds. Any advice?
Have you tried using UIInterfaceOrientationPortrait/UIInterfaceOrientationLandscape? I usually have pretty good results using these methods:
if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) {
//do portrait work
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){
//do landscape work
}
You might also try UIDeviceOrientationIsValidInterfaceOrientation.
These will only return the app's orientation, which may not be the same as the device's orientation.
If those don't work, you can also try:
[[UIApplication sharedApplication] statusBarOrientation]

Detecting iPad launch orientation

I'm developing an app that will feature a splash screen fading to the first app page. This splash screen is supposed to seamlessly flow from the Default-X.png image from the app launch. I've got this working great, except for one very special situation.
If the user taps the app icon, then IMMEDIATELY changes orientation, the automatic Default-X.png will come up from the original orientation (as expected), but my programmatically-defined intro image comes up in the new orientation. (I guess this is expected, too, now that I think about it.)
My question is, how can I get the actual launch orientation. NOT the orientation available when the app delegate starts, but the orientation of the device when the app icon is tapped, and therefor the orientation the OS uses to decide on the Default-X.png image.
Thanks.
I spent hours on this, then I found this and it saved me.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

Resources