We write the interface to consider a lot of user experience problems, keyboard event response is a more troublesome one. We need to listen to keyboard events, consider a click on the background to pick up the keyboard, consider the keyboard occlusion input box problems, and so on, and every interface to do so. This library has helped us to solve this problem.
of this library: Https://github.com/hackiftekhar/IQKeyboardManager
This library is a singleton, and once it comes into effect, any interface to the whole project is valid. The code that makes it work can be written anywhere, I write it in appdelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { IQKeyboardManager *manager = [IQKeyboardManager sharedManager]; manager.enable = YES; manager.shouldResignOnTouchOutside = YES; manager.shouldToolbarUsesTextFieldTintColor = YES; manager.enableAutoToolbar = NO; returnYES;}
Enable controls whether the entire feature is enabled.
Shouldresignontouchoutside controls whether the click Background is closed.
Shouldtoolbarusestextfieldtintcolor controls whether the toolbar text color on the keyboard is user-definable.
Enableautotoolbar controls whether the toolbar on the keyboard is displayed.
The above setting, the library is enabled.
Using the method, the code is as follows:
#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) IQKeyboardReturnKeyHandler *returnKeyHandler;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self]; self.returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyDone; self.returnKeyHandler.toolbarManageBehaviour = IQAutoToolbarBySubviews;}- (void)dealloc{ self.returnKeyHandler = nil;}@end
To set the Returnkeyhandler, you can click on the next key on the keyboard to automatically jump to the next input box. Last input box click Done to automatically close the keyboard.
After running, you can see that the input box automatically floats up and down as the keyboard pops up. Click on the background and the keyboard is closed. It's fully automatic.
This library is supported by default Uitextfield, Uitextview, UIWebView, Uiscrollview, UITableView,Uicollectionview
Finally, it is important to note that it can automatically calculate the order of multiple TextField, arranged according to the order of the Addsubview.
Third-party libraries that iOS automatically handles keyboard events: Iqkeyboardmanager