Custom Keyboard: inputView: how to change the Keyb

2019-01-22 14:48发布

问题:

I implemented the textfield with a custom keyboard with the "setInputView" function. But i have a problem: my keyboard frame is not a standard iphone keybord frame.

The question is: How can i change the size of my custom keyboard? I know some functions like: UIKeyboardFrameBeginUserInfoKey, ..etc.

Please Note: The iPhone keyboard frame is = 0,264,320,216 My custom keyboard frame is = 0,0,320,460

Hoping for your kind collaboration, Best regards... P

回答1:

It turns out that the default behaviour of the custom input view that you assign to the UITextField's property is to resize the view to the same frame as the default keyboard. Try setting (I use the name InputViewController for my input view, but you can use whatever you want):

inputViewController = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:nil];
inputViewController.delegate = self;
inputViewController.view.autoresizingMask = UIViewAutoresizingNone; // This is the code that will make sure the view does not get resized to the keyboards frame.

For more detailed information, you can look at this link, which is provided by Apple.:

If UIKit encounters an input view with an UIViewAutoresizingFlexibleHeight value in its autoresizing mask, it changes the height to match the keyboard.

Hope that Helps!



回答2:

To set the keyboard inputView with the same size as the native keyboard just do this:

inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

To set your own frame do this:

inputView.autoresizingMask = UIViewAutoresizingNone;

From Apple:

You have a lot of flexibility in defining the size and content of an input view or input accessory view. Although the height of these views can be what you’d like, they should be the same width as the system keyboard. If UIKit encounters an input view with a UIViewAutoresizingFlexibleHeight value in its autoresizing mask, it changes the height to match the keyboard. There are no restrictions on the number of subviews (such as controls) that input views and input accessory views may have. For more guidance on input views and input accessory views, see iOS Human Interface Guidelines.



回答3:

I had the same problem. I solved it by registering for UIKeyboardDidShowNotification (UIKeyboardWillShowNotification did not work, unfortunately) and then changing the view size after the keyboard was shown. However, it still had the white box on top of the keyboard when it was moving up. This worked fine for me because it is coming in over a UITextView with a white background. If you were coming in over any other colored objects, however, it would look a little ugly before the view was properly resized. You can solve that by setting the background color to clearColor.

// Add this while initializing your view
self.backgroundColor = [UIColor clearColor]; // Needed because we can't resize BEFORE showing the view.  Otherwise you will see an ugly white box moving up w/ the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];



// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{

    CGRect rect = self.frame;
    rect.size.height = 164;
    self.frame = rect;

}


回答4:

For me msgambel's solution didn't work. But the approach right, I was playing with the inputView's autoresizingMask. Former I had different setting, but the right way to avoid white extra space over the custom keyboard is:

I applied this just for the outermost view.



回答5:

Also if you're using a UIViewController to design your inputView, don't use the UIViewController.view... it seems to have a lot of problems getting resized incorrectly on rotate regardless of the AutoresizeMask.

What worked for me was to take my existing UI and use Editor > Embed In > View. Then create a new outlet, and pass that outlet as the inputView. Suddenly the resize on rotate bugs disappeared.