tags:

views:

251

answers:

5

I've noticed that I'm getting very intermittent orientation on my device & the simulator.

I have a modal view controller that I present, and that is the only thing in my app which supports rotation.

If I launch the app in portrait without moving the device, open the modal VC and then rotate the device, it usually works. However sometimes if I open the app holding the device in landscape, then rotate to portrait, launch the VC and then rotate the device, no rotation occurs. It seems very intermittent. Sometimes if I launch the app in portrait mode and then open the VC and rotate the device, nothing happens, and until I quit and relaunch it no orientation occurs in the app.

It's strange because 50% of the time it works! Whenever I launch it through Xcode and set breakpoints in shouldAutorotateToInterfaceOrientation it always works!

Anyone ever had this or know what's going on?

A: 

Even if you try to launch your application in landscape, your phone will launch it in portrait and then go into landscape depending in which position your phone is.

I don't know what code you are using to accomplish this. Below is one which is taken from Apple code snippets. See if this can help.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {    
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);  
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = 
        CGAffineTransformMakeRotation(degreesToRadian(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
}
Leo
Thanks for the answer. This appears to be code to swap between two separate portrait and landscape views. I'm simply allowing the rotation of a single view, purely by returning YES in the shouldAutorotateToInterfaceOrientation method.
Michael Waterfall
A: 

I've had similar challenges getting autorotation to work properly for view controllers whose parent view controllers don't autorotate, although most of my experience has related to wrangling UINavigationController as opposed to modal view controllers. Still, I'd recommend trying the following:

  1. Call presentModalViewController: on the top level view controller in your hierarchy rather than a deeper viewController.

  2. If that doesn't solve it, try subclassing your top level view controller, and overriding its shouldAutorotateToInterfaceOrientation: to look something like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (self.modalViewController) {
        return [self.modalViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
cduhn
+1  A: 

Since as you mentioned "intermittent", i would say it has something to do with what are you doing after the rotation.

To find the bug, i suggest removing any code after the rotation happens. Comment out any network activities or OpenGL operations. Could also help to close XCode and reopen it.

If nothing helps, i would make a new project and start moving the files one by one and test.

medopal
Yeah I definitely think something strange is going on. I plan on stripping out code until it's working and dissecting it that way. Thanks for the suggestion.
Michael Waterfall
A: 

Does calling

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

when you show your rotation-aware view help? (and possibly turning it off again when you dismiss it?)

The other thing I would look for is suspicious calls to [UIWindow makeKeyWindow] or [UIResponder becomeFirstResponder].

I'd also sign up for UIDeviceOrientationDidChangeNotification events and make sure that the Modal view got a call to [UIViewController shouldAutorotateToInterfaceOrientation:] for each event you receive.

nivekastoreth
Thanks for the help. I'll try out the suggestions and get back to you!
Michael Waterfall
A: 

I've finally discovered what the problem was!

It turns out that only the very first UIView you add to your UIWindow will get asked whether or not to rotate. Any subsequent views don't receive this message. My app was using another view to animate away from the Default.png that is displayed on launch, and this was added to the window first!

Michael Waterfall