iOS: Device orientation on load
Asked Answered
A

16

56

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.

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

Alinaaline answered 4/5, 2011 at 18:16 Comment(1)
See also iPhone orientationPronoun
W
65

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 sharedApplication] statusBarOrientation];

Original answer

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

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

- (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.

Let us know how it works out.

Willard answered 4/5, 2011 at 19:17 Comment(7)
Yes, checking the statusbarOrientation is what did the trick!Alinaaline
Note that the message is named statusBarOrientation (not statusbarOrientation with a lowercase 'b') - edit submitted for review.Allyson
not working for me I am getting portrait every time.I am trying it for Ipad and testing on simulator.Does it depend on hardware.Will it work on device?Systematize
this is not working: the device can be in a different orientation as the status bar if the parent view has not rotated to the current orientation.Escritoire
shouldAutorotateToInterfaceOrientation will differ depending on whether root vc is a navigation controller or not.Boer
see this post. #5888516Canary
look this solution for load time device oriantation .... jenishshah2311.blogspot.in/2015/09/…Canary
J
11

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

Jeannettajeannette answered 4/5, 2011 at 18:28 Comment(1)
That did't seem to work, I ended up using [UIApplication sharedApplication].statusBarOrientation which works.Alinaaline
C
4

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

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.

 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
    }
    }
Crackpot answered 22/3, 2018 at 22:41 Comment(1)
I did get a message saying that statusBarOrientation was deprecated. Maybe you can update your answer with the new values?Dragonet
B
2

One thing that nobody has touched on yet is that you’re storing UIDeviceOrientation types in a UIInterfaceOrientation variable. They are different, and should not be treated as equal. Note that UIDeviceOrientationLeft is equal to UIInterfaceOrientationRight (since the interface rotates the opposite way compared to the device).

Bushwhack answered 3/10, 2011 at 1:33 Comment(1)
Yep, deviceorientation include facedown and faceup. iOS 6 is a stepping-stone to fixing device/interface orientation issues. iOS < 6 was stupidly complicated, and still very broken as of iOS 6.0.1. The new -(BOOL)shouldAutorotate is never called, at least in the simulator. And shouldAutorotateToInterfaceOrientation was deprecated prematurely.Boer
M
2

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.

Margrettmarguerie answered 13/11, 2012 at 7:39 Comment(0)
D
2

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

    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 portraitOrientation always 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.

Disfigurement answered 5/7, 2016 at 8:18 Comment(0)
H
1

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.

Heth answered 6/3, 2015 at 21:24 Comment(0)
P
1

Swift 3 based on @Marjin 's code.

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

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

if(portraitOrientation)
{
     // Portrait
}
else
{

}
Plater answered 4/5, 2017 at 15:14 Comment(0)
K
0

Try the accelerometer to get its reading, UIAccelerometer, get sharedAccelerometer, set its delegate, get the readings, figure out from there orientation.

Kilar answered 4/5, 2011 at 18:17 Comment(2)
Yeah, but older iPod touch's don't have an Accelerometer, so this won't work in my case.Alinaaline
actually even the first ipod touch had an accelerometerZellers
B
0

Tried all and no good results. So what I did, as I'm on an ipad, was to leave all the work to the splitViewController methods to invalidate the barButton:

For portrait:

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc { NSlog(@"portrait");}

For landscape:

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ NSlog(@"landscape");}

this always works on load.

Bethanybethe answered 7/11, 2011 at 19:31 Comment(0)
F
0

the problem is that [UIDevice currentDevice]orientation] sometimes reports the the device's orientation incorrectly.

instead use [[UIApplication sharedApplication]statusBarOrientation] which is a UIInterfaceOrientation so to check it you'll need to use the UIInterfaceOrientationIsLandscape(orientation)

hope this helps.

Fadeout answered 9/5, 2012 at 16:7 Comment(1)
Under what circumstance is the device's orientation reported incorrectly? Can you cite an example?Teletypewriter
C
0

I still use this working code snippet for iphone 4:

-(void)deviceOrientationDidChange:(NSNotification *)notification{

//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

int value = 0;

if(orientation == UIDeviceOrientationPortrait)
{
    value = 0;

}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
    value = 90;

}else if(orientation == UIDeviceOrientationLandscapeRight)
{

    value = -90;

}

CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value));
[photoImageView setTransform:cgCTM];

}

Caecum answered 30/5, 2012 at 12:32 Comment(0)
B
0

This is the real answer. When an app starts up, it's orientation is unknown. It uses shouldAutorotateToInterfaceOrientation and supportedInterfaceOrientations to decide which orientation to pick.

Watch as I start up a sample app in the iPhone 5.0 simulator and rotate it using the code below and 'Supported interface orientations' with all 4 possible orientations:

20:44:08.218 RotationTestApp Supported orientation: Portrait
20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)

I've seen lots of code snippets, but none of them work generically enough (iPad & iPhone, iOS 5.0+).

Instead of fumbling around with try-this-try-that, place the following in your root vc:

#define ToNSString_BEGIN(T) \
NSString* T##ToNSString(T valueParameter) { \
switch (valueParameter) {

#define ToNSString_VALUE(value) \
case value: return @#value

#define ToNSString_END(T) \
} \
return @"(unknown)"; \
}

// NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation);
ToNSString_BEGIN(UIInterfaceOrientation);
ToNSString_VALUE(UIInterfaceOrientationPortrait);           // 1
ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2
ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft);      // 3
ToNSString_VALUE(UIInterfaceOrientationLandscapeRight);     // 4
ToNSString_END  (UIInterfaceOrientation);

// NSString* UIDeviceOrientationToNSString(UIDeviceOrientation);
ToNSString_BEGIN(UIDeviceOrientation);
ToNSString_VALUE(UIDeviceOrientationUnknown);               // 0
ToNSString_VALUE(UIDeviceOrientationPortrait);              // 1
ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown);    // 2
ToNSString_VALUE(UIDeviceOrientationLandscapeLeft);         // 3
ToNSString_VALUE(UIDeviceOrientationLandscapeRight);        // 4
ToNSString_VALUE(UIDeviceOrientationFaceUp);                // 5
ToNSString_VALUE(UIDeviceOrientationFaceDown);              // 6
ToNSString_END  (UIDeviceOrientation);



// Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms.
- (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
{
    NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation);
    return interfaceOrientationAsMask & [self supportedInterfaceOrientations];
}

// Reads from the project's-Info.plist
- (NSUInteger)supportedInterfaceOrientations
{
    static NSUInteger orientationsResult;

    if (!orientationsResult) {
        NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];

        for (id orientationString in supportedOrientations) {
            if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortrait;
                NSLog(@"Supported orientation: Portrait");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown;
                NSLog(@"Supported orientation: Portrait (upside-down)");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeRight;
                NSLog(@"Supported orientation: Landscape (home button on the left)");
            } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft;
                NSLog(@"Supported orientation: Landscape (home button on the right)");
            } else {
                NSLog(@"Unrecognized orientation '%@' in mainBundle plist, key UISupportedInterfaceOrientations", orientationString);
            }
        }
    }
   return orientationsResult;
}

// iOS 6+ (not yet used in 6.0.1)
- (BOOL)shouldAutorotate
{
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
    BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice];
    NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice);
    NSLog(@"shouldAutorotate: %s (current orientation %@)", result ? "YES" : "NO", currentDeviceOrientation);
    return result;
}

// iOS 2.0 - 5.1 (iOS 6+ deprecated, 6.0.1 still works)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSString* orientationString;
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;

    if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) {
        orientationString = [NSString stringWithFormat:@"current device orientation: %@, interface orientation wants: %@",
                             UIDeviceOrientationToNSString(interfaceOrientationFromDevice),
                             UIInterfaceOrientationToNSString(interfaceOrientation)
                             ];
    } else {
        orientationString = [NSString stringWithFormat:@"device orientation: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice)
                             ];
    }

    BOOL result = [self allowAutoRotate:interfaceOrientation];
    NSLog(@"shouldAutorotateToInterfaceOrientation: %s (%@)",
          result ? "YES" : "NO",
          orientationString);
    return result;
}

There is still a nagging problem of segue animations not using the current orientation. My guess is that subclassing each VC and putting some orientation on push / notify delegate on pop would be the way to go.

Also important:

shouldAutorotateToInterfaceOrientation doesn't work

tabBarController and navigationControllers in landscape mode, episode II

Boer answered 13/1, 2013 at 5:8 Comment(0)
C
0

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.

UIInterfaceOrientation orientation = [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");
}
Canary answered 24/9, 2015 at 9:54 Comment(0)
R
0

Try This one [[UIApplication sharedApplication] statusBarOrientation];

or implement this one in app delegate

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

it works

Reft answered 17/2, 2016 at 12:42 Comment(0)
F
-1

Everyone above posted very valid answers : but as an UPDATE: Apple's take : you should e using UIStatusBar orientations to read current orientation of the device:

One way you could check the current orientation of the device is by using an int values as such, inside the viewDidLoad method :

    int orientationType = [[UIDevice currentDevice] orientation];

where consider the following . . . - 1 = portrait (right way up) - 2 = portrait upside down - 3 = landscape (right) - 4 = landscape (left)

and then you could use an IF statement to call a method after orientation is detected, so on and so forth:

Hope this was slightly helpful to someone

Firebox answered 19/9, 2013 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.