iOS给键盘添加控制栏

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,而是有多种情况,所以通过监听系统事件

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上变成了透明的,这样导致的结果就会如下情况,而且这时候的控制栏是不能点击,英系那个美观

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 01:17:09

iOS给键盘添加控制栏的相关文章

iOS 为键盘添加隐藏按钮

// 为键盘添加隐藏按钮 UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; [backView setBarStyle:UIBarStyleDefault]; UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSp

iOS开发项目篇—04添加导航栏的按钮

iOS开发项目篇—04添加导航栏的按钮 一.设置导航栏的按钮 要求实现的效果:             说明:默认状态下和高亮状态下的图片是不一样的. 按钮的图片需要设置默认状态和高亮状态时的显示,系统了提供的下面方法 viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#>

iOS开发项目—04添加导航栏的按钮

iOS开发项目—04添加导航栏的按钮 一.设置导航栏的按钮 要求实现的效果:             说明:默认状态下和高亮状态下的图片是不一样的. 按钮的图片需要设置默认状态和高亮状态时的显示,系统了提供的下面方法 viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#>

iOS 7 教程:定制iOS 7中的导航栏和状态栏

目录(?)[-] iOS 7中默认的导航栏 设置导航栏的背景颜色 在导航栏中使用背景图片 定制返回按钮的颜 修改导航栏标题的字体 修改导航栏标题为图片 添加多个按钮 修改状态栏的风格 隐藏状态栏 总结 注:本文译自Customizing Navigation Bar and Status Bar in iOS 7 近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这

iOS 7中的导航栏和状态栏

iOS 7中默认的导航栏 在开始定制之前,我们先来看看iOS 7中默认导航栏的外观.通过Xcode用Single View Controller模板创建一个工程.然后将view controller嵌入到一个navigation controller中.如果你不想从头开始,那么也可以在这里下载到这个 示例工程.Xcode 5包含有iOS 6和iOS 7模拟器,我们可以在这两个不同的模拟器版本中运行示例程序,进行对比,如下图所示: 如上图所示,在iOS 7中的导航栏默认情况下跟状态栏是交织在一起的

定制iOS 7中的导航栏和状态栏

本文转载至 http://www.cocoachina.com/industry/20131104/7287.html 跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也 “” 阅读器 近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这

WEB网页输入框的默认键盘类型控制

参考资料 http://www.w3school.com.cn/html5/att_input_type.asp : 语法 <input type="value"> 属性值 值 描述 button 定义可点击的按钮(大多与 JavaScript 使用来启动脚本) checkbox 定义复选框. color 定义拾色器. date 定义日期字段(带有 calendar 控件) datetime 定义日期字段(带有 calendar 和 time 控件) datetime-lo

IOS 实现自定义的导航栏背景以及自定义颜色的状态栏(支持7.0以及低版本)

为尊重文章原作者,转载务必注明原文地址:http://www.cnblogs.com/wt616/p/3784717.html 先看效果图: 在自定义导航栏背景时,可能会遇到以下一些问题: 1.当设置导航栏背景后,状态栏的颜色也会跟着一起改变掉,这可能不是你说希望看到的 2.IOS7以上的版本和低版本显示出来的导航栏高度位置有差别,这个差别就是状态栏的高度20,为了兼容低版本,必须统一 解决思路: 1.不正常的是状态栏的背景也一起变了,而状态栏的文字是可以通过其他API去设置的:如 [[UIAp

精通 VC++ 实效编程280例 - 03 控制栏 [转]

Windows 应用程序的控制栏包括工具栏和状态栏等.在工具栏中可以添加按钮和控件,实现快捷操作.在状态栏中可以显示一些提示信息.MFC 中,控制栏中的工具栏和状态栏分别通过 CToolBar 类和 CstatusBar 类实现,它们的父类都为 CContorlBar 类. 33 创建工具栏 创建工具栏,首先创建一个工具栏资源,然后构造一个 CToolBar 类对象,接下来调用 CToolBar::CreateEx 函数创建工具栏窗口,最后调用 CToolBar::LoadToolBar 函数加