您的位置:首页 > 移动开发 > IOS开发

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上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: