IOS adds a control bar to the keyboard
In iOS, the keyboard is frequently used. Sometimes you can add a control bar to the keyboard to conveniently switch or hide the keyboard in different input boxes.
The specific implementation method is briefly described here.
Initialize a UIToolBar and add it to the interface. The height of the keyboard changes dynamically for display.
The code implementation is as follows:
Header file
# Import
# Import
@ Interface UIKeyboardTool: NSObject // toolbar @ property (nonatomic, strong) UIToolbar * toolBar for interface display; // array for cursor switching @ property (nonatomic, strong) NSArray * fieldArray; // display the keyboard-(void) showToolBar :( UITextField *) field; @ end
Initialization
-(Instancetype) init {if (self = [super init]) {CGFloat screenHeight = [[UIScreen mainScreen] bounds]. size. height; CGFloat screenWidth = [[UIScreen mainScreen] bounds]. size. width; toolBar = [[UIToolbar alloc] initWithFrame: CGRectMake (0, screenHeight, screenWidth, 44)]; optional * nextItem = [[UIBarButtonItem alloc] initWithTitle: @ next style :( response) target: self action: @ selector (showNext)]; UIBarButtonItem * previusitem = [[UIBarButtonItem alloc] initWithTitle: @ previous style :( UIBarButtonItemStylePlain) target: self action: @ selector (showPrevious)]; UIBarButtonItem * spaceItem = [[financialloc] failed :( partial) target: self action: nil]; UIBarButtonItem * doneItem = [[UIBarButtonItem alloc] initWithTitle: @ hide style :( UIBarButtonItemStylePlain) target: self action: @ selector (showHide)]; toolBar. items = @ [nextItem, previusitem, spaceItem, doneItem]; // listen for keyboard height changes [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (keyboardFrameChange :) name: Invalid object: nil]; currentField = nil;} return self ;}
Keyboard display
/// Display the keyboard-(void) showToolBar :( UITextField *) field {currentField = field; [UIView beginAnimations: nil context: nil]; [UIView setAnimationDuration: 0.25]; CGFloat screenHeight = [[UIScreen mainScreen] bounds]. size. height; CGFloat screenWidth = [[UIScreen mainScreen] bounds]. size. width; [toolBar setFrame: CGRectMake (0, screenHeight-300, screenWidth, 44)]; [UIView commitAnimations];}
Switch the input box
/// Click Next-(void) showNext {NSInteger current = [fieldArray indexOfObject: currentField]; if (current + 1) <fieldArray. count) {UITextField * field = [fieldArray objectAtIndex: current + 1]; [field becomeFirstResponder] ;}}
With the update of IOS, the keyboard height of iOS is not 216 in iOS5, but in many cases.
UIKeyboardWillChangeFrameNotification
To dynamically handle height changes. The value returned by the notification is an NSDictionary
The values are as follows:
/// Animation curve type UIKeyboardAnimationCurveUserInfoKey = 7; // animation duration UIKeyboardAnimationDurationUserInfoKey = 0.25; UIKeyboardBoundsUserInfoKey = NSRect: {0, 0}, {375,258 }}; /// UIKeyboardCenterBeginUserInfoKey = NSPoint: {187.5, 796} at the start of the keyboard animation; // UIKeyboardCenterEndUserInfoKey = NSPoint: {187.5, 538} at the end of the keyboard animation }; /// the size of UIKeyboardFrameBeginUserInfoKey = NSRect :{{ 0,667 },{ 375,258 }}; // the size of UIKeyboardFrameEndUserInfoKey = NSRect when the keyboard animation ends: {0,409 },{ 375,258 }}; // whether the keyboard is displayed, bool type, 1 is displayed, and 2 is hidden. UIKeyboardIsLocalUserInfoKey = 1;
Based on the final bool Value
/// Modify frame-(void) keyboardFrameChange :( NSNotification *) using Y {NSDictionary * dict = [using Y userInfo]; NSValue * endValue = [dict objectForKey: UIKeyboardFrameEndUserInfoKey]; CGRect endFrame = [endValue CGRectValue]; [UIView beginAnimations: nil context: nil]; [UIView setAnimationDuration: 0.25]; CGFloat screenHeight = [[UIScreen mainScreen] bounds]. size. height; CGFloat screenWidth = [[UIScreen mainScreen] bounds]. size. width; NSNumber * isShowKeyboardValue = [dict objectForKey: @ UIKeyboardIsLocalUserInfoKey]; BOOL isShowKeyboard = isShowKeyboardValue. boolValue; if (isShowKeyboard) {// change the keyboard height [toolBar setFrame: CGRectMake (0, endFrame. origin. y-44, screenWidth, 44)];} else {// hide the keyboard [toolBar setFrame: CGRectMake (0, screenHeight, screenWidth, 44)];} [UIView commitAnimations];}
How to Use
Initialization
tool = [[UIKeyboardTool alloc] init]; tool.fieldArray = @[field,field1,field2]; [self.view addSubview:tool.toolBar];
Display
-(void)textFieldDidBeginEditing:(nonnull UITextField *)textField{ [tool showToolBar:textField];}
As iOS9 is updated, when the word association interface is opened, the background of the previous version is not transparent, but the iOS9 is transparent, the result is as follows, and the control bar cannot be clicked at this time.