开发者

How can I manually rotate the keyboard (With animation)?

I have a controller that manages a UITextView. Normally, in an app that supports multiple interface orientations, I would simply开发者_开发知识库 override -shouldAutorotateToInterfaceOrientation:, set the appropriate auto-resizing masks, and go along my merry way. While going along my merry way, if I were to begin editing this UITextField in, say, portrait mode, then rotate the device to a landscape orientation, the keyboard would animate nicely to landscape mode along with the rest of the view. This is what I want.

Now, in this particular app, I have a root view that must only ever be in portrait mode. This is a camera preview view. I also have an overlay-view for the camera-view that I would like to have support all interface orientations. This is where the UITextView is located.

So, as a result of the fixed-orientation root view, I cannot use built in rotation: I have to do this manually. This isn't a problem, I can register for the UIDeviceOrientationDidChangeNotification and manually animate the appropriate rotation and frame adjustment for the overlay view.

That's good enough to get the view positioned correctly, but the keyboard would still only ever show in portrait orientation. Again, I can correct this with UIApplication's -setStatusBarOrientation: every time I get a notification that the device has rotated.

But that's where the problem arises. I would hope that the combination of:

  • Register for rotation notifications
  • Manually rotate view
  • Set status bar orientation (with animation)

would be enough to exactly replicate the automatic rotation behavior. But it falls short in one important way: If the keyboard were to be shown in one orientation, then rotated to another, the keyboard would remain defiantly in the original position, as if -setStatusBarOrientation: were never called. As far as I know, the only thing that affects the keyboard's presentation is the application's status bar orientation, and there is only the one method for setting it. So am I out of luck? Is it really impossible to make the keyboard rotate with a view manually like it does with auto-rotation?

Addendum

In an effort to avoid covering old ground, I've tried the following solutions hacks:

  • Upon orientation change, resign and immediately become first responder again. This does make the keyboard move to the appropriate orientation, but it does so without animation, and occasionally does really bad things like show a landscape-sized keyboard in portrait and vice-versa.
  • Upon orientation change just resign first responder. This also has the odd behavior of instantly moving (but not resizing) the keyboard to the new orientation, then dismissing it with animation. It's very ugly and jarring.


One of our apps does this (because we didn't say "no!" loudly enough when presented with the original design) using crazy "orientation stack" code. I remember fixing this problem by disabling orientation when the keyboard is up (ickyyyy).

Instead, consider supporting rotation by "counter-rotating" the camera preview view instead of forcing the VC to portrait-only. If this looks a bit odd, you can do the "nasty hack" version: Add the camera preview view as a subview of the window, under your VC's view, and give your VC's view a transparent background.


Maybe late to the party. I stumbled upon the same problem. Trying to fix it in iOS 6.

Apparently Apple didn't recognize this problem in the meanwhile, otherwise they would've fixed it by now: have the -setStatusBarOrientation: method include other interface elements besides only the status bar as well, or have alternative methods to set the orientation of the keyboard and other elements. A pity they didn't

Disabling orientation rotation when the keyboard is up, is no solution for me, but I found another simple solution hack:

  • return NO in -(BOOL)shouldAutorotate
  • return UIInterfaceOrientationMaskAll in -(NSUInteger)supportedInterfaceOrientations
  • listen for device rotation notifications

Then when you receive such notification:

  • hide the status bar
  • when the keyboard is up, dismiss the keyboard ([myTextField resignFirstResponder])
  • perform manual rotation animation
  • set the orientation of the statusbar ([sharedApplication setStatusBarOrientation:newOrientation animated:NO])
  • show the status bar
  • if the keyboard was visible before, present it again ([myTextField becomeFirstResponder])

The effect is that the status bar and the keyboard gently slide out before the manual rotation animation and gracefully slide back in, when the animation is done.

BTW, I also found this interesting article by Corey Floyd: http://coreyfloyd.tumblr.com/post/8212203583/2-ways-to-rotate Although his second method: Tricking UIKit, only caused more confusion with me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜