views:

492

answers:

3

Hello,

I’m trying to set up an iPad test application, window-based, where I have a single view controller and a single view. When I rotate the iPad, I want the orientation of the toolbar to change, but not that of the view itself. For example, a sort of background view that you work in is fixed to the device, but the status bar and toolbars rotate around it. This would enable the user to work the view from all angles, but always with a correctly-oriented toolset.

A beautiful implementation of what I want can be found in the Brushes for iPad app, where the painting’s orientation is locked to the device, and the toolbars rotate around it. I think other painting apps do the same thing.

I’ve been trying to figure out how to do this, but after exhausting many many other questions here concerning orientation, I’m still at a loss.

Could anyone point me in the right direction towards a neat solution? A particular combination of autoresizes for the autoresizeMask? Countering the rotation animation with another one in the opposite direction? Using multiple concurrent view controllers, one for the rotating views and one for the non-rotating ones?

I’d very much appreciate it,

(Edit: Attempted to clarify the question, after Olie’s comment.)

A: 

To prevent rotation, you'd put this in your view controller's .m:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

But you say you still want the view frame to resize in response to the rotation. I haven't had a need to do this myself, so I'm not sure if it's sufficient to just set the autoresizingMask to have flexible width and height; you may also have to implement didRotateFromInterfaceOrientation: and use setNeedsLayout and/or resize the view manually.

Shaggy Frog
Thank you for responding, but controlling a single view's rotation is really not the problem here. What I want is to keep the main view fixed, and the status bar and toolbars rotating around it when the orientation changes. To get the entire screen either rotating or not is peanuts, the issue at hand is different rotation behaviour for different parts of the same screen.
WanderWeird
A: 

To the extent I have worked with I cannot see any simple answer to your question. What about rotating everything (tabbar, nav and status bar, your view controller) and then redrawing the content of your view controller in "old coordinates" so for the user it will look like it's not rotated?

Kostiantyn Sokolinskyi
A: 

I had a bug that did this a while back -- I'm pretty sure that what you're asking will get you a HIG-violation rejection from Apple. However, I'll give a shot at remembering what the problem was. I'm pretty sure it was something like this:

I had a tabbarViewController that said "I orient to any orientation."

One of the tabs was a regular-old UIViewController that said "I only do LandscapeLeft & L-Right"

When you rotated, the inside (UIVC) stayed put, but the outside (TabVC) rotated around things.

I might have some of the details backwards or otherwise convoluted, but the general ideas is: stacked VCs, not all one VC.

Good luck!

Olie
Thank you for your answer. I actually got it to work a while ago. Someone pointed out the solution to me: In the `-loadView` method of the view controller whose view you want to stay fixed, use the `UIWindow`’s `-insertSubview:belowSubview:` method, inserting the non-rotating view below the navigation controller’s view. That works exactly right.
WanderWeird
Ah, there you go. Very similar idea, you just attached a 2nd view to the window, whereas I stacked view controllers. In general, I tend to try to have exactly one view (with its view controller) in the window, then everything else subview-ed from that. I'm not sure why, but it just feels "cleaner" to me. Regardless, your way works -- and isn't awful -- and that's what matters.
Olie