0

I have the following code in my UIViewController subclass, however the willAnimateROtationToInterface is never called. Any idea why?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

UPDATE: The parent's view controller orientation changes delegate is getting called however not this one. I added this view controller as a subview of the parent's view controller

The parent view controller is a UINavigationController, so here's how I am adding the subview:

[self.navController.view addSubview:viewController.view];
xonegirlz
  • 8,889
  • 19
  • 69
  • 127
  • Is the user interface rotating? – Ell Neal Jan 23 '12 at 00:11
  • yes the user interface is rotating – xonegirlz Jan 23 '12 at 00:22
  • And is this the currently visible view controller? i.e. has the view for the view controller in question been loaded? – Ell Neal Jan 23 '12 at 00:26
  • yes it's the currently visible – xonegirlz Jan 23 '12 at 00:31
  • Right... Can you provide some more information about the hierarchy of your application? Are you using storyboards or nib files? Are you setting the rootViewController of your keyWindow? What's the parent view controller of the view controller you're having trouble with? – Ell Neal Jan 23 '12 at 00:36
  • well.. I guess the parent's view controllers willAnimateRotation is getting called, but not this one.. so how do I adjust stuff inside this view controller? – xonegirlz Jan 23 '12 at 00:48
  • So is this view controller being shown as a subview of another view controller? – Ell Neal Jan 23 '12 at 00:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6949/discussion-between-ell-neal-and-xonegirlz) – Ell Neal Jan 23 '12 at 00:59

2 Answers2

1

Do you have any of the following methods present in your UIViewController subclass?

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration;

If so you are going down the two-part path, and the one-part method willAnimateRotationToInterfaceOrientation:duration: will not be called.

UPDATE: In response to xonegirlz's update above:

The parent navigation controller (the UINavigationController) has no knowledge of your UIViewController - you have linked the views together in the view hierarchy, but not the view controllers together in the controller hierarchy.

If you are targetting iOS 5, you can call -[UIViewController addChildViewController:] on the UINavigationController to inform it of the child.

For iOS 4, you need to use -[UINavigationController pushViewController:animated: or -[UIViewController presentModalViewController:animated:] to setup the proper view controller hierarchy.

If that's not an option, you need to subclass the parent view controller, and manually forward the rotation methods to your child view controller.

iccir
  • 5,078
  • 2
  • 22
  • 34
  • If you implement -willRotateToInterfaceOrientation:duration: and -didRotateFromInterfaceOrientation and have them NSLog(@"will rotate") and NSLog(@"did rotate"), do the logs show up? – iccir Jan 23 '12 at 00:21
  • they don't ... that's basically my question – xonegirlz Jan 23 '12 at 00:31
  • Note: those two methods are different from -willAnimateRotationToInterfaceOrientation:duration . I'm sadly out of ideas. If you add a log to -shouldAutorotateToInterfaceOrientation: above, is it getting called? – iccir Jan 23 '12 at 00:39
  • hmm.. shouldAutorotate is called once when the view loads only – xonegirlz Jan 23 '12 at 00:43
  • That's an interesting clue - in my UIViewController subclass it gets called each time I change the orientation of the device. – iccir Jan 23 '12 at 00:46
  • http://stackoverflow.com/questions/2868132/shouldautorotatetointerfaceorientation-doesnt-work "Make sure all of your parent views have autoresizesSubviews = YES". Is this in a tab bar? – iccir Jan 23 '12 at 00:50
  • it still doesn't call it.. the parent view controller is a UINavicationController – xonegirlz Jan 23 '12 at 00:58
  • I just saw your update above - in this case, the parent navigation controller (the UINavigationController) has no knowledge of your UIViewController - you have linked the views together in the view hierarchy, but not the controllers together in the controller hierarchy. If you are targetting iOS 5, you can call `-[UIViewController addChildViewController:]` on the UINavigationController to inform it of the child. For iOS 4, you need to use -pushViewController:animated: or -presentModalViewController:animated: to get the proper hierarchy. – iccir Jan 23 '12 at 01:06
  • If that's not an option, make a custom UINavigationController subclass, and forward the rotation methods to your custom UIViewController subclass. – iccir Jan 23 '12 at 01:07
1

It turned out this was an issue with view controller containment, i.e. the UIViewController was being nested inside another custom UIViewController subclass, which was not forwarding the notifications (or using iOS5's UIViewController containment methods).

Ell Neal
  • 6,014
  • 2
  • 29
  • 54