开发者

UITextField Custom Keyboard and Response to Device Orientation

开发者 https://www.devze.com 2023-03-06 00:46 出处:网络
I\'m trying to attach a custom keyboard to a UITextField, and the keyboard would respond to different view orientations (landscape, portrait, etc). In short, the issue is that while the keyboard does

I'm trying to attach a custom keyboard to a UITextField, and the keyboard would respond to different view orientations (landscape, portrait, etc). In short, the issue is that while the keyboard does show up, when responding to view orientation when I rotate the device, the dimensions of the keyboard is all messed up, and I'm not quite sure how to set the keyboard frame/bounds correctly. Hope someone here can give me a hand!

Here is my design:

The keyboard is built from a subclass of UIViewController (from xib). There are two views: landscapeKeyboardView and portraitKeyboardView.

The keyboard is attached to the UITextField through the property "inputView".

The rotation of the keyboard view is done in the function:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration
{
    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) 
    {
        self.view = self.landscapeKeyboardView;
    }else if((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {
        self.view = self.portraitKeyboardView;
    }
}

The rotation does occur, but the view dimension is all messed up after the rotation. I have also tried to manually set the frame or the bounds of the view of either self.view or landscapeKeyboardView or portraitKeyboardView, and none o开发者_开发百科f them seemed to solve the problem.

Does anyone know how to work around this issue? Or is there a much better design pattern for custom keyboards?

Thanks!


You probably need to translate the CGRect for rotation. UIView has a number of instance methods for converting rects and points. It accounts for views outside the frame of another view (to find out how far below a UIPopover the keyboard rect is, for example) as well as rotation. convertRect:fromView: is just one example, but they all start with 'convert'.

0

精彩评论

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