iOS键盘监控键盘
2015-07-03 14:28
543 查看
iOS中键盘的使用很频繁,有时给键盘上添加一个控制栏可以方便快捷的在不同输入框内进行切换或隐藏
这里简单说下具体实现方式
初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示
下面来看代码实现
头文件部分
[objc] view
plaincopy
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIKeyboardTool : NSObject
///用于界面展示的toolbar
@property (nonatomic,strong) UIToolbar *toolBar;
///用于光标切换的数组
@property (nonatomic,strong) NSArray *fieldArray;
///显示键盘
-(void)showToolBar:(UITextField *)field;
@end
初始化
[objc] view
plaincopy
-(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)];
UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showNext)];
UIBarButtonItem *previousItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showPrevious)];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:(UIBarButtonItemStylePlain) target:self action:@selector(showHide)];
toolBar.items = @[nextItem,previousItem,spaceItem,doneItem];
///监听键盘高度变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
currentField = nil;
}
return self;
}
键盘显示
[objc] view
plaincopy
///显示键盘
-(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];
}
输入框切换
[objc] view
plaincopy
///点击下一个
- (void)showNext
{
NSInteger current = [fieldArray indexOfObject:currentField];
if ((current + 1) < fieldArray.count)
{
UITextField *field = [fieldArray objectAtIndex:current + 1];
[field becomeFirstResponder];
}
}
随着IOS的更新,iOS的键盘高度不在是iOS5中的216,而是有多种情况,所以通过监听系统事件
[objc] view
plaincopy
UIKeyboardWillChangeFrameNotification
来动态处理高度变化,通知返回的值是一个NSDictionary
这里面的值如下
[objc] view
plaincopy
///动画曲线类型
UIKeyboardAnimationCurveUserInfoKey = 7;
///动画持续时间
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
///键盘动画起始时的中心点
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
///键盘动画结束时的中心点
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
///键盘动画起始时的大小
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
///键盘动画结束时的大小
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
///键盘是否是展示,bool类型,1展示,2隐藏
UIKeyboardIsLocalUserInfoKey = 1;
根据最后的bool值来判断
[objc] view
plaincopy
///动态更改frame
-(void)keyboardFrameChange:(NSNotification *)notify
{
NSDictionary *dict = [notify 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)
{
///键盘高度更改
[toolBar setFrame:CGRectMake(0, endFrame.origin.y - 44 , screenWidth, 44)];
}
else
{
///键盘隐藏
[toolBar setFrame:CGRectMake(0, screenHeight, screenWidth, 44)];
}
[UIView commitAnimations];
}
如何使用
初始化
[objc] view
plaincopy
tool = [[UIKeyboardTool alloc] init];
tool.fieldArray = @[field,field1,field2];
[self.view addSubview:tool.toolBar];
展示
[objc] view
plaincopy
-(void)textFieldDidBeginEditing:(nonnull UITextField *)textField
{
[tool showToolBar:textField];
}
遇到的问题,随着iOS9的更新,当打开单词联想界面的时候,之前的版本背景是不透明的,但是iOS9上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
这里简单说下具体实现方式
初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示
下面来看代码实现
头文件部分
[objc] view
plaincopy
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIKeyboardTool : NSObject
///用于界面展示的toolbar
@property (nonatomic,strong) UIToolbar *toolBar;
///用于光标切换的数组
@property (nonatomic,strong) NSArray *fieldArray;
///显示键盘
-(void)showToolBar:(UITextField *)field;
@end
初始化
[objc] view
plaincopy
-(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)];
UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showNext)];
UIBarButtonItem *previousItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:(UIBarButtonItemStylePlain) target:self action:@selector(showPrevious)];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:(UIBarButtonItemStylePlain) target:self action:@selector(showHide)];
toolBar.items = @[nextItem,previousItem,spaceItem,doneItem];
///监听键盘高度变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
currentField = nil;
}
return self;
}
键盘显示
[objc] view
plaincopy
///显示键盘
-(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];
}
输入框切换
[objc] view
plaincopy
///点击下一个
- (void)showNext
{
NSInteger current = [fieldArray indexOfObject:currentField];
if ((current + 1) < fieldArray.count)
{
UITextField *field = [fieldArray objectAtIndex:current + 1];
[field becomeFirstResponder];
}
}
随着IOS的更新,iOS的键盘高度不在是iOS5中的216,而是有多种情况,所以通过监听系统事件
[objc] view
plaincopy
UIKeyboardWillChangeFrameNotification
来动态处理高度变化,通知返回的值是一个NSDictionary
这里面的值如下
[objc] view
plaincopy
///动画曲线类型
UIKeyboardAnimationCurveUserInfoKey = 7;
///动画持续时间
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
///键盘动画起始时的中心点
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
///键盘动画结束时的中心点
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
///键盘动画起始时的大小
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
///键盘动画结束时的大小
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
///键盘是否是展示,bool类型,1展示,2隐藏
UIKeyboardIsLocalUserInfoKey = 1;
根据最后的bool值来判断
[objc] view
plaincopy
///动态更改frame
-(void)keyboardFrameChange:(NSNotification *)notify
{
NSDictionary *dict = [notify 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)
{
///键盘高度更改
[toolBar setFrame:CGRectMake(0, endFrame.origin.y - 44 , screenWidth, 44)];
}
else
{
///键盘隐藏
[toolBar setFrame:CGRectMake(0, screenHeight, screenWidth, 44)];
}
[UIView commitAnimations];
}
如何使用
初始化
[objc] view
plaincopy
tool = [[UIKeyboardTool alloc] init];
tool.fieldArray = @[field,field1,field2];
[self.view addSubview:tool.toolBar];
展示
[objc] view
plaincopy
-(void)textFieldDidBeginEditing:(nonnull UITextField *)textField
{
[tool showToolBar:textField];
}
遇到的问题,随着iOS9的更新,当打开单词联想界面的时候,之前的版本背景是不透明的,但是iOS9上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
相关文章推荐
- IOS开发-使用Storyboard进行界面跳转及传值
- iOS 字符转换为16进制如@"139989"转成<139989>
- iOS 读取图片的各种方法
- 实战配置Nagios主机及服务实战
- iOS关于block使用的注意和探讨
- atomic和nonatomic区别
- IOS学习笔记——ViewController生命周期
- iOS 如何不让自动锁屏
- Nagios 监控twemproxy
- iOS 沙盒目录下的文件的读写。
- IOS之Info.plist文件简介
- ios 远程推送教程
- ios 图片旋转 雷达效果如何让一副图片绕某一个点来旋转
- iOS 笔试题
- iOS--NSString---过滤换行符和空白字符
- iOS 自动计算cell高度
- iOS 9 适配系列教程
- iOS开发系列--通知与消息机制
- iOS开发小技巧之--WeakSelf宏的进化
- iOS tableview cell 中自定义lable