I have a UIView root and a UIView A as its subview
I don't allow root to be rotated, so I write in root class
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;}
but I allow the root's subview (A) to be rotated, so in A class, I write
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;}
But it seems that both root and its subview A can't rotate.
How should I allow A to be rotated but root not?
thanks
Views don't answer to shouldAutorotateToInterfaceOrientation, view controllers do. If your view controller says it will rotate then its view and all child views will rotate. If your view controller says it won't rotate then its view won't rotate and neither will its children.
There are two possible approaches to solving your problem, depending on exactly what you want to do:
- if the non-rotating view is behind everything that rotates, you could remove it from the view controller and add it to the main application window behind the view controller upon viewWillAppear: and remove it again on viewDidDisappear:
- if the non-rotating and rotating views need to be intermingled in some more complicated way, you can catch willAnimateRotationToInterfaceOrientation:duration: and set a suitable inverse transform on the non-rotating view. So its rotated and then unrotated. You'll need to be a bit tricky to get correct behaviour between certain 180 degree rotations because of the way CoreAnimation works.
UIApplication
provides a means to get the key window as a UIWindow object, which is probably what you'd want for the first idea. UIWindow inherits from UIView, so all the standard mechanisms for adding and arranging subviews apply.
For the latter, you'll want to set the transform on the relevant view. Get the transform from the superview, get the inverse and set it on the view. See the CGAffineTransform for more specifics. The CoreAnimation issue alluded to is that the transition from one rotation to one 180 degrees different by the shortest route (as CoreAnimation will take) gives two potential solutions — one clockwise and one anticlockwise. In practice, you need to add an invisible bias to the view's transform when in portrait mode to ensure CoreAnimation picks the right one if you're rotating between portrait and portrait upside down. If you're on the iPhone, it's quite acceptable not to support UIInterfaceOrientationPortraitUpsideDown
on that device (most of the Apple apps don't), so it's actually probably easier just not to support that orientation.