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