iOS键盘监控键盘

iOS中键盘的使用很频繁,有时给键盘上添加一个控制栏可以方便快捷的在不同输入框内进行切换或隐藏

这里简单说下具体实现方式

初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示

下面来看代码实现

头文件部分

[objc]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import <UIKit/UIKit.h>  
  3. @interface UIKeyboardTool : NSObject  
  4.   
  5. ///用于界面展示的toolbar  
  6. @property (nonatomic,strongUIToolbar *toolBar;  
  7. ///用于光标切换的数组  
  8. @property (nonatomic,strongNSArray *fieldArray;  
  9.   
  10. ///显示键盘  
  11. -(void)showToolBar:(UITextField *)field;  
  12.   
  13. @end  

初始化

[objc]  view plain copy
  1. -(instancetype)init  
  2. {  
  3.     if (self == [super init])  
  4.     {  
  5.       
  6.         CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;  
  7.         CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;  
  8.           
  9.         toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, screenHeight, screenWidth, 44)];  
  10.           
  11.           
  12.         UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showNext)];  
  13.         UIBarButtonItem *previousItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showPrevious)];  
  14.         UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];  
  15.         UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:(UIBarButtonItemStylePlain) target:self action:@selector(showHide)];  
  16.           
  17.         toolBar.items = @[nextItem,previousItem,spaceItem,doneItem];  
  18.           
  19.         ///监听键盘高度变化  
  20.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];  
  21.   
  22.         currentField =  nil;  
  23.           
  24.     }  
  25.     return self;  
  26. }  

键盘显示

[objc]  view plain copy
  1. ///显示键盘  
  2. -(void)showToolBar:(UITextField *)field  
  3. {  
  4.     currentField = field;  
  5.       
  6.     [UIView beginAnimations:nil context:nil];  
  7.     [UIView setAnimationDuration:0.25];  
  8.     CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;  
  9.     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;  
  10.     [toolBar setFrame:CGRectMake(0, screenHeight-300, screenWidth, 44)];  
  11.     [UIView commitAnimations];  
  12.   
  13. }  

输入框切换

[objc]  view plain copy
  1. ///点击下一个  
  2. - (void)showNext  
  3. {  
  4.     NSInteger current = [fieldArray indexOfObject:currentField];  
  5.   
  6.     if ((current + 1) < fieldArray.count)  
  7.     {  
  8.         UITextField *field = [fieldArray objectAtIndex:current + 1];  
  9.         [field becomeFirstResponder];  
  10.     }  
  11. }  

随着IOS的更新,iOS的键盘高度不在是iOS5中的216,而是有多种情况,所以通过监听系统事件
[objc]  view plain copy
  1. UIKeyboardWillChangeFrameNotification  

来动态处理高度变化,通知返回的值是一个NSDictionary

这里面的值如下

[objc]  view plain copy
  1. ///动画曲线类型  
  2. UIKeyboardAnimationCurveUserInfoKey = 7;  
  3. ///动画持续时间  
  4. UIKeyboardAnimationDurationUserInfoKey = "0.25";  
  5. UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";  
  6. ///键盘动画起始时的中心点  
  7. UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";  
  8. ///键盘动画结束时的中心点  
  9. UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";  
  10. ///键盘动画起始时的大小  
  11. UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";  
  12. ///键盘动画结束时的大小  
  13. UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";  
  14. ///键盘是否是展示,bool类型,1展示,2隐藏  
  15. UIKeyboardIsLocalUserInfoKey = 1;  

根据最后的bool值来判断

[objc]  view plain copy
  1. ///动态更改frame  
  2. -(void)keyboardFrameChange:(NSNotification *)notify  
  3. {  
  4.     NSDictionary *dict = [notify userInfo];  
  5.     NSValue *endValue = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];  
  6.       
  7.     CGRect endFrame = [endValue CGRectValue];  
  8.       
  9.       
  10.     [UIView beginAnimations:nil context:nil];  
  11.     [UIView setAnimationDuration:0.25];  
  12.     CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;  
  13.     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;  
  14.    
  15.     NSNumber *isShowKeyboardValue = [dict objectForKey:@"UIKeyboardIsLocalUserInfoKey"];  
  16.     BOOL isShowKeyboard = isShowKeyboardValue.boolValue;  
  17.       
  18.       
  19.     if (isShowKeyboard)  
  20.     {  
  21.         ///键盘高度更改  
  22.         [toolBar setFrame:CGRectMake(0, endFrame.origin.y - 44 , screenWidth, 44)];  
  23.   
  24.     }  
  25.     else  
  26.     {  
  27.         ///键盘隐藏  
  28.         [toolBar setFrame:CGRectMake(0, screenHeight, screenWidth, 44)];  
  29.   
  30.     }  
  31.       
  32.     [UIView commitAnimations];  
  33.   
  34. }  

如何使用

初始化

[objc]  view plain copy
  1. tool = [[UIKeyboardTool alloc] init];  
  2. tool.fieldArray = @[field,field1,field2];  
  3. [self.view addSubview:tool.toolBar];  

展示

[objc]  view plain copy
  1. -(void)textFieldDidBeginEditing:(nonnull UITextField *)textField  
  2. {  
  3.     [tool showToolBar:textField];  
  4. }  

遇到的问题,随着iOS9的更新,当打开单词联想界面的时候,之前的版本背景是不透明的,但是iOS9上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值