Detecting device rotation

Discussion in 'iOS Programming' started by rand0m3r, May 20, 2008.

  1. macrumors regular

    Jun 30, 2006
    right now i'm doing this:

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

    but it doesn't detect an iphone that is rotated (in the simulator)? how do i actually pick up the notifcations?
  2. macrumors 6502a


    Jan 4, 2002
    Austin, TX
    There is a ton of sample code for iPhone that covers all of this fundamental stuff.

    Try looking in the iPhone dev site. The sample code are complete working solutions covering various topics so you don't have to ask a forum on a Mac rumors web site.
  3. thread starter macrumors regular

    Jun 30, 2006
    i've looked and it doesn't work, which is why i'm here fool.
  4. macrumors 6502

    Jan 16, 2008
    Nottingham, England
    I've just tested on beta 5 and the notification definitely is generated in the simulator.

    Here's my code. Out of interested, I added this to the WhichWayUp sample so I could see the crate turning (which will help you test it on real hardware).

    - (void) didRotate:(NSNotification *)notification
       UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
       if (orientation == UIDeviceOrientationLandscapeLeft)
           NSLog(@"Landscape Left!");
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
       [window addSubview:crateViewController.view];
       [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
       [[NSNotificationCenter defaultCenter] addObserver:self
          name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    That is in the AppDelegate class. Let us know how you get on!

    P.S. I don't think the samples cover using the notification (WhichWayUp seems to use some built-in UIView magic - the rotation appears to happen automatically in the View).
  5. macrumors regular

    Feb 27, 2006
    Kent, United Kingdom
    When you come to a forum to ask for help, you might want to think about being polite to people who are trying to help you and not be such a cock.
  6. macrumors newbie

    Apr 12, 2008
    Minor change

    There is already a predefined constant for the device orientation notification, which you should use in place of the literal string in case Apple decides to change it in the future.

       [[NSNotificationCenter defaultCenter] addObserver:self
          name:[B]UIDeviceOrientationDidChangeNotification[/B] object:nil];
  7. macrumors newbie

    Jun 26, 2008
    Problem when Rotating

    Hi all,

    I am implementing orientation in my application view. But when my device rotate to left or right with that my image is also rotates but that time a blank black view is display in 1/4 part of the screen, i don't know why.
    I am just doing
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    return YES;

    I am attaching the screen which i am getting after rotation, in that the left corner black frame is displaying after rotation.

    Please help me how can i solve this problem.

    Thanks in Advance,

    Attached Files:

  8. macrumors member

    Apr 9, 2007
    Have you/anyone got the answer for the same problem.
    Please let me know.
  9. macrumors 68030


    Sep 2, 2008
    You need to set the autoResizeMask so that the view is resized correctly when its superview is resized. Set flexibleWidth and flexibleHeight for the view.
  10. macrumors member

    Apr 9, 2007
    As the shouldrotate.. method is not called so we have written in loadview method but no effect :eek:(.
    Please help

Share This Page