Keyboard masking input controls or buttons to avoid in the daily development of the app, consider the various cases of UI nesting, and finally register to listen and then get the specified keyboard information. We can use the keyboard processing tool class Zykeyboardutil to avoid the complex, using block to deal with the keyboard cover problem.
Demo Address: Https://github.com/liuzhiyi1992/ZYKeyboardUtil
The main response:
Screen Keyboard masking input control issues
Third-party keyboard pop-up pits multiple times
View multi-layered nested inner input control processing
Multiple input control processing in a single page
Zykeyboardutil registers the keyboard via lazy mode to notify listeners that a block automatically handles keyboard occlusion events (processing/recovery), plus two custom action blocks for the user to handle.
The inner class Keyboardinfo acts as the model stores the keyboard information needed for each processing.
Configure Animatewhenkeyboardappearautomaticanimblock to handle the issue of keyboard occlusion input control in full zykeyboardutil. Included in the custom action block, Animatewhenkeyboardappearblock for keyboard display processing, animatewhenkeyboarddisappearblock as the keyboard when the processing, Instead, Printkeyboardinfoblock is used to output keyboard information when necessary. Appearblock and Disappearblock Unified the uiviewanimation, only need to write the required interface changes.
Screen test:
Multiple layers of input control nesting:
Third-party keyboard pop-up issues:
How to use:
In our controller, create a Zykeyboard object that allows your controller to hold it for the duration of the entire page implementation function.
1 |
self.keyboardUtil = [[ZYKeyboardUtil alloc] init]; |
Handling Keyboard Events:
Configure Animatewhenkeyboardappearautomaticanimblock to fully automate keyboard lifting and occlusion input control problems, and automatically restore controls after they are resignfirstresponder. Simply call Adaptiveviewhandlewithcontroller:adaptiveview using the parameter Keyboardutil in the BLOCK: the first parameter is the current page controller, the second parameter receives a variable parameter, A view of a single/multiple input control or package input control within the current page.
1234 |
__weak ViewController *weakSelf = self; [_keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) { [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.inputViewOne, weakSelf.inputViewSecond, weakSelf.inputViewThird, weakSelf.inputViewFourth, nil]; }]; |
The following is an additional custom action block:
1234 |
//展开事件 [_keyboardUtil setAnimateWhenKeyboardAppearBlock:^(int appearPostIndex, CGRect keyboardRect, CGFloat keyboardHeight, CGFloat keyboardHeightIncrement) { //do something when keyboard appear }]; |
Appearpostindex is when the keyboard is expanded (third-party keyboards are split) to record the number of increases that are currently in the same deployment.
The Keyboardrect is the rect of the keyboard position after this expansion.
Keyboardheight the keyboard height after this expansion.
Keyboardheightincrement the increment of the keyboard for this expansion. (Key to third-party keyboard expansion).
1234 |
//收起事件 [_keyboardUtil setAnimateWhenKeyboardDisappearBlock:^(CGFloat keyboardHeight) { //do something to restor UI when keyboard dismiss }]; |
Keyboardheight the height of the keyboard before it is closed.
1234 |
//获取键盘信息 [_keyboardUtil setPrintKeyboardInfoBlock:^(ZYKeyboardUtil *keyboardUtil, KeyboardInfo *keyboardInfo) { //you can get keyboardInfo here when animation ended }]; |
The keyboard information Keyboardinfo can be obtained in the parameters of this block.
Be careful not to forget to avoid the block's circular reference by weakself: __weak viewcontroller *weakself = self;
Zykeyboardutil automatic handling of keyboard occlusion events