IOS keyboard management tool IQKeyboardManager

There will be a variety of almost all applications in the input box, such as landing when the need to enter the account number and password, often appear in the list of the search box, we fill in the delivery address when shopping and so on, these common needs are first in the input box, and then to verify the information submitted, but not forget there is a step input, the submission process of keyboard operation.
on the keyboard, we probably in tone from the hidden learning iOS will be exposed to early development, there are several commonly used methods:
1 on the current controller View add a tap gesture, when clicking on the screen to determine the input controls in the page is firstResponder, the use of resignFirstResponder on it.
2 when the keyboard bounce click on the lower right corner of the keyboard button.
3 when the interface has UITableView, sliding tableView hide.
well, through these methods we can achieve the perfect demand, when necessary, the keyboard, the input can be completed by clicking, sliding and other operations to hide the keyboard. But when you add a new page, we need to re write the above logic, really have enough trouble, fortunately, we have IQKeyboardManager, IQKeyboardManager iOS keyboard is a management tool, it is a function of only two lines of code can be realized in the project all of the page click on the screen to hide the keyboard if you want to import, do not need to add a line of code can be realized after the package, of course it’s not only these, then I will introduce some of our commonly used functions. At the end of the article I will write about the IQKeyboardManager source code analysis, as far as possible to know that they know why.

1 click on the screen to hide the keyboard

IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; keyboardManager.shouldResignOnTouchOutside = YES;

Now you can have all the page click on the screen to hide the keyboard, and so on, can not say in front of a line of code does not need to add it, very simple, open the IQKeyboardManager.m file in keyboardManager.shouldResignOnTouchOutside (+) added = YES load method; modified

(void) load{IQKeyboardManager. [[IQKeyboardManagersharedManager] setEnable:YES]; [IQKeyboardManagersharedManager].shouldResignOnTouchOutside = YES;}

The principle is very simple, reference IQKeyboardManager header files when you + (load) method will be executed automatically, the original code for initialization in this method, we only need to add and click on the screen to operate it.

2 keyboard input box when the page automatically move up

This feature is now available to you… IQKeyboardManager also supports handwritten code and StoryBoard, when your input box is covered by the keyboard will automatically move up the page. The move up distance is controlled by the property keyboardDistanceFromTextField.

KeyboardManager.keyboardDistanceFromTextField = 100;

It is worth noting that when you use StoryBoard to build the page, if the input top bound control point to Top Layout Guide when the function is invalid, the StoryBoard Top Layout Guide ViewController as an attribute, which represents a desire not to be the highest position of the status bar and navigation bar cover. When there is only Status Bar when the location of the Status Bar below, when the presence of Navigation Bar when the location below Navigation Bar. So when the top constraint of the control points to it, the control position cannot be moved. Solution: the top constraint point to Superview.Top steps: Editer -> pin -> View
IOS keyboard management tool IQKeyboardManager
: BA2E999B-2A91-46FA-9FBE-BC883FD197FD.png

3 add toolbar for keyboard

IQKeyboardManager will automatically add a toolbar to the keyboard, which includes a left arrow, a right arrow, an input box, and a Done button to close the keyboard.
can be turned on / off by enableAutoToolbar

Toolbar keyboardManager.enableAutoToolbar = NO / / close;

4.Navigation Bar display

If you Wang always show UINavigationBar, then change UIView Class To to the UIScrollView.

If the page with a UINavigationBar page, UINavigationBar will move together out of the screen frame, the author provides a method of substitution UIScrollView controller View, but in a few cases occur control disorder and other side effects, so the use of this method should be careful when

Source analysis

1 monitor notification

In the IQKeyboardManager initialization by monitoring the keyboard, UItextField, UITextViewField notification to complete the control of the interface

2 add gestures

IQKeyboardManager created a tap gesture in the initialization time, when we click the textField call action
notice by monitoring UITextFieldTextDidBeginEditingNotification – (void) textFieldViewDidBeginEditing: (NSNotification*) notification; in this way the tap gesture is added to the current UITextField.window, in the action method tap gesture called hiding operation on the keyboard

3 displacement is covered by the interface

Find the top of stack in viewController from UITextField.window.rootViewController in UIKeyboardWillShowNotification, UITextFieldTextDidBeginEditingNotification when listening to, and then calculate the input box and the distance of keyboard, you can now modify the viewController.view top to complete the page up, similarly, when receiving UIKeyboardWillHideNotification, UITextFieldTextDidEndEditingNotification notification time will move after the reduction of view to its original position.

Write to the end: IQKeyboardManager as a maintenance project for several years is far more than these, interested students can download to the Demo, which shows all the features of the github. The logic of this project is clear, powerful, for learning and use have brought me a lot of help, thanks to the author has brought us such a good open source projects.