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];
}
}
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;
///动态更改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];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhwezhwe/article/details/46726239