Set keyboard orientation only (iOS 6)

2019-01-25 23:30发布

my app has been working fine until I've tried making some changes using the ios 6 SDK

The app runs in portrait mode 99% of the time. This has been constrained by only allowing portait mode to be available in the info.plist

There is one view controller which needs to be shown in landscape mode. This is achieved "manually" by simply rotating the view by 90 degrees, like so:

self.view.transform = CGAffineTransformMakeRotation(3.14159/2);

This still works fine in iOS 6.

However, this view controller has some text fields. When the user taps one it shows the keyboard. Since I've only rotated the view (and not actually changed the orientation of the device), the keyboard comes out in portrait mode, which is no good.

In previous versions of iOS, I set the orientation of the status bar to be landscape, and as a byproduct, this would set the keyboard to be landscape as well, like this:

[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];

However, this has stopped working for iOS 6.

I have read a million stack overflows trying to get this to work, but still have no luck.

标签: ios ios6
1条回答
Luminary・发光体
2楼-- · 2019-01-26 00:25

Changing the keyboard orientation and transform is an difficult part and not a good solution(especially when it changes status bar orientations).

Better solutions is to allow application to support all orientations.

enter image description here

Implement the Orientation delegates inside your ViewControllers asper the rotation support.

For Supporting only Landscape

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
    || interfaceOrientation == UIInterfaceOrientationLandscapeRight ;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

For Supporting only Portrait

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
查看更多
登录 后发表回答