iOS给键盘添加控制栏
2015-07-02 14:26
435 查看
iOS中键盘的使用很频繁,有时给键盘上添加一个控制栏可以方便快捷的在不同输入框内进行切换或隐藏
这里简单说下具体实现方式
初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示
下面来看代码实现
头文件部分
#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
初始化
-(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;
}
键盘显示
///显示键盘
-(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];
}
输入框切换
///点击下一个
- (void)showNext
{
NSInteger current = [fieldArray indexOfObject:currentField];
if ((current + 1) < fieldArray.count)
{
UITextField *field = [fieldArray objectAtIndex:current + 1];
[field becomeFirstResponder];
}
}
随着IOS的更新,iOS的键盘高度不在是iOS5中的216,而是有多种情况,所以通过监听系统事件
来动态处理高度变化,通知返回的值是一个NSDictionary
这里面的值如下
///动画曲线类型
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值来判断
///动态更改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];
}
如何使用
初始化
tool = [[UIKeyboardTool alloc] init];
tool.fieldArray = @[field,field1,field2];
[self.view addSubview:tool.toolBar];
展示
-(void)textFieldDidBeginEditing:(nonnull UITextField *)textField
{
[tool showToolBar:textField];
}
遇到的问题,随着iOS9的更新,当打开单词联想界面的时候,之前的版本背景是不透明的,但是iOS9上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
这里简单说下具体实现方式
初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示
下面来看代码实现
头文件部分
#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
初始化
-(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;
}
键盘显示
///显示键盘
-(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];
}
输入框切换
///点击下一个
- (void)showNext
{
NSInteger current = [fieldArray indexOfObject:currentField];
if ((current + 1) < fieldArray.count)
{
UITextField *field = [fieldArray objectAtIndex:current + 1];
[field becomeFirstResponder];
}
}
随着IOS的更新,iOS的键盘高度不在是iOS5中的216,而是有多种情况,所以通过监听系统事件
UIKeyboardWillChangeFrameNotification
来动态处理高度变化,通知返回的值是一个NSDictionary
这里面的值如下
///动画曲线类型
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值来判断
///动态更改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];
}
如何使用
初始化
tool = [[UIKeyboardTool alloc] init];
tool.fieldArray = @[field,field1,field2];
[self.view addSubview:tool.toolBar];
展示
-(void)textFieldDidBeginEditing:(nonnull UITextField *)textField
{
[tool showToolBar:textField];
}
遇到的问题,随着iOS9的更新,当打开单词联想界面的时候,之前的版本背景是不透明的,但是iOS9上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 在 Linux 下交换 Ctrl 与大写锁定键
- CS 1.5 键盘基本操作快捷键
- C#控制键盘按键的常用方法
- C#检查键盘大小写锁定状态的方法
- 按键盘方向键翻页跳转的javascript代码(支持ie,firefox)
- C++处理键盘输入的方法
- 键盘 keycode的值 javascript时触发事件时很有用的要素
- js鼠标、键盘事件实例代码
- 如何让div span等元素能响应键盘事件操作指南
- 键盘扫描码[比较完整]
- javascript 响应键盘特定按键(只响应数字键)
- javascript键盘上下键的操作(选择)
- 使用钩子如何锁定键盘的方法分享