订制ios系统键盘的特定按钮
在IOS项目开发中,常遇到一些特殊的需要,比如需要特殊的字符或按钮显示在系统键盘的某个位置,那么这时候就要想到自定义一个按钮,来满足需要,在本人目前的项目开发中正好有这样一个需要:当用户输入密码后,点击键盘的Done按钮登录,但要求是系统键盘的Done 按钮是一个中文提示,如“登录”或“下一步”等。
通过阅读相关资料和测试,在前辈们的基础上做了一些修正和扩展,比如可以在多个文件中创建自定义的类(该类已经封装)对象,此外也利用Block进行方法回调,增强了封装性和健壮性。
具体实现步骤如下;
第一步,创建一个可重用的继承类,代码如下:
// 头文件
#import <UIKit/UIKit.h>
typedef void(^DoneActionBlock)(id); // 定义一个Block
@interface ChangeDoneKeyButtonTextField : UITextField
@property (nonatomic, retain) UIButton* doneButton;
@property (nonatomic, retain) NSString *buttonTitle;
@property (nonatomic, copy) DoneActionBlock doneEven;
@end
// 实现文件
#import “ChangeDoneKeyButtonTextField.h”
@implementation ChangeDoneKeyButtonTextField
@synthesize doneButton;
@synthesize buttonTitle;
@synthesize doneEven;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 注册UIKeyboardDidShowNotification通知的观察者为self
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification object:nil];
}
return self;
}
// 实现观察者的注册事件
-(void)keyboardDidHide:(NSNotification*)note{
if (self.doneButton) {
self.doneButton.hidden=YES;
}
}
- (void)keyboardDidShow:(NSNotification *)note {
//keyboard位于顶层窗口
NSInteger topWindow = [[[UIApplication sharedApplication] windows] count] – 1;
UIWindow *keyboard = [[[UIApplication sharedApplication] windows] objectAtIndex:topWindow];
// 在键盘第1次弹出时,创建按钮
if (self.doneButton == nil) {
self.doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.doneButton.hidden=YES;
// 向键盘中加入按钮
[keyboard addSubview:self.doneButton];
//定制按钮上面要显示的文字,也可以在创建该对象时自定义,本文只自定义了按钮上面的显示文字为:buttonTitle
[self.doneButton setTitle:buttonTitle forState:UIControlStateNormal]; // 设置按钮的位置在恰当的地方
[self.doneButton setFrame:CGRectMake(486/2.0, 876/2.0, 152/2.0, 80/2.0)];
[self.doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
// 设置按钮背景图片
[self.doneButton setBackgroundImage:[UIImage imageNamed:@"DoneKeyBackground.png"] forState:UIControlStateNormal];
// 当按钮按下时,触发doneButton方法
[self.doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
}
if (self.editing) { // 只有当前输入框会显示done按钮
self.doneButton.hidden = NO;
} else {
self.doneButton.hidden = YES;
}
[keyboard bringSubviewToFront:self.doneButton];
}
//定制按钮的点击事件
- (void)doneButton:(id)sender {
self.doneButton.hidden=YES;
[self resignFirstResponder];
if (doneEven!=nil) { // block 回调
doneEven(sender);
}
}
- (void)dealloc
{
self.doneButton = nil;
self.doneEven = nil;
self.buttonTitle = nil;
[super dealloc];
}
第二步,在需要使用定制键盘的类中进行创建输入框对象,代码如下:
// 需要定制键盘文件的头文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@end
// 需要定制键盘文件的实现文件
#import “ViewController.h”
#import “ChangeDoneKeyButtonTextField.h”
@interface ViewController ()
{
ChangeDoneKeyButtonTextField *changeDoneField;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 定制文本输入框
changeDoneField = [[[ChangeDoneKeyButtonTextField alloc] initWithFrame: CGRectMake(60, 60, 200, 30)] autorelease];
changeDoneField.borderStyle = UITextBorderStyleRoundedRect;
changeDoneField.keyboardType = UIKeyboardTypeDefault;
changeDoneField.returnKeyType = UIReturnKeyDone;
changeDoneField.delegate = self;
changeDoneField.placeholder = @”定制Done按钮键盘”;
changeDoneField.buttonTitle = @”登录”; //自定义按钮上面的文字,也可以是位置等该按钮的相关属性
changeDoneField.doneEven = ^(id sender){ //实现Block的回调
NSLog(@”begin logining”); //触发按钮的点击事件
};
[self.view addSubview:changeDoneField];
// 普通文本输入框
UITextField *testField = [[UITextField alloc] initWithFrame:CGRectMake(60, 110, 200, 30)];
testField.borderStyle = UITextBorderStyleRoundedRect;
testField.keyboardType = UIKeyboardTypeDefault;
testField.returnKeyType = UIReturnKeyDone;
testField.delegate = self;
testField.placeholder = @”系统默认键盘”;
[self.view addSubview:testField];
[testField release];
}
#pragma -mark textFieldDelegate
// 实现键盘的代理
- (void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField == changeDoneField) {
changeDoneField.doneButton.hidden = NO;
}else {
changeDoneField.doneButton.hidden = YES;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == changeDoneField) {
changeDoneField.doneButton.hidden = YES;
}else {
changeDoneField.doneButton.hidden = NO;
}
}
@end
至此,自定义键盘代码全部完成,见下图一,在测试中也遇到一个问题,那就是如果用户点击了手写,那么定制的按钮同样会显示出来,见图二,这个问题还待研究,至此,定制常用键盘的功能,满足基本需要已经实现,在此次测试过程中,特别感谢同事李瑞勤的帮助。
附效果图:
图一
图二
订制ios系统键盘的特定按钮相关推荐
- ios 数字键盘左下角添加按钮_ios数字键盘添加完成按钮
ios数字键盘添加完成按钮,示代码如下(附件中有效果图和示例工程代码): // // ViewController.h // KeyboardTest // 自定义数字键盘,添加完成按钮 // Cre ...
- 改变iOS系统自带返回按钮事件
最近要用到 用 系统自带的带箭头的返回按钮 返回到指定的ViewController 或者返回到RootViewController, 自定义NaviBarItem也可以 但是想用系统的箭头,就偶然 ...
- ios 数字键盘左下角添加按钮_IOS数字键盘左下角添加完成按钮的实现方法
IOS数字键盘左下角添加完成按钮的实现方法 实现代码: - (void)addDoneButtonToNumPadKeyboard { UIButton *doneButton = [UIButton ...
- iOS系统键盘和自定义键盘的切换
// 1. 给UITextView添加一个可点击的UIControlUIControl *control = [[UIControl alloc] initWithFrame:_inputView.b ...
- ios 数字键盘左下角添加按钮_iOS8数字键盘加左下角完成button
iOS8数字键盘加左下角完成button的核心代码如下面: - (void)addDoneButtonToNumPadKeyboard { UIButton *doneButton = [UIButt ...
- 苹果首款AR眼镜曝光:搭载订制ios,却不支持wifi...
在谷歌眼镜问世7年.Oculus Rift首次亮相4年之后,苹果似乎终于准备好了自己的智能眼镜. 一直以来,苹果公司在增强现实技术方面的抱负在iphone和ipad上已经很清晰地体现出来了.每一年,苹 ...
- 关于xcode7下的ios模拟器输入内容无法显示系统键盘的解决办法
xcode7下的ios模拟器输入内容无法系统键盘,只能用电脑键盘输入内容,这样可能会对调试带来麻烦. 其实xcode7下的ios模拟器默认只能使用一种,要么是模拟器系统键盘,要么就是是电脑键盘.设置方 ...
- iOS - 监控键盘高度、屏蔽字符、限制字符个数
iOS开发中,经常会用到UITextView,UITextField输入文本操作,默认的键盘输入会有emoji表情.空格.非法字符等,会与实际的产品需求不符,这时候就需要我们做限制,禁止输入表情或者空 ...
- Android 键盘的搜索按钮功能
系统键盘的搜索按钮,默认情况下是被隐藏的,如果要使用必须要手动设置,才可以调用搜索按键功能. 具体使用,只需要如下三个步骤: 1:在布局文件中的EditText中添加如下三个属性 android:ma ...
最新文章
- Java多线程协作(wait、notify、 notifyAll)
- 基于deep learning的快速图像检索系统
- 【EXLIBRIS】随笔记 006
- mysql concat $_mysql concat 的诡异问题
- Eclipse中JBOSS5.1无法启动的问题解决办法
- 【华为云技术分享】一文讲清C语言核心要点
- Character Studio
- 对称加密之AES算法的python实现
- UI自动化测试篇 :ReportNG替代TestNG自带html版测试报告初探
- 微信小程序获取用户头像和昵称能力调整!新的代替方案!
- exchange 2010部署之五
- 三菱plc指令dediv_三菱plc指令tcmp的用法
- 谈谈架构师是何种生物
- linux 卸载lxde,卸载KDE / Xfce / LXDE 回到纯Unity 的状态
- c++商店信息管理系统的设计与实现
- RTC保存年份不正确的问题排查及解决
- linux服务器cpu过高满载问题
- [视频]QQ空间技术架构之深刻揭秘------技术体会
- 嵌入式linux 声控,采用压电陶瓷片的声控照明灯
- ZTE Axon 20 5G Phone Review
热门文章
- 【Genshin私服】Grasscutter部署全流程
- SSM毕设项目乡村不动产租赁系统0sie7(java+VUE+Mybatis+Maven+Mysql)
- Jzoj5644 凫趋雀跃
- 11.29 电驴设置对话框形式的实现中遇到的问题
- Asp.net WebAPI 教程整理
- java.lang.IllegalAccessException: void #####.MyBroadcastReceiver.init() is not accessible from jav
- java体系学习总结记录——超长篇
- IPv4数据报格式和Ipv6数据报格式(长文,详细解释每个字段)
- 实现一个简单的python自动化测试框架
- Python对文件进行批量重命名