iOS中如何自定制悬浮键盘iOS6

iOS 自定义键盘
代码地址如下:
少年佳节倍多情,老去谁知感慨生;
不效艾符趋习俗,但祈蒲酒话升平。
鬓丝日日添白头,榴锦年年照眼明;
千载贤愚同瞬息,几人湮没几垂名。
UI想要的效果,我…只能默默的承受着
端午将近,想想再过一个小时就要回家,心里难免有点激动,但是作为一个程序猿,怎么也不能闲着,于是想起了最近正在做的项目中关于自定义键盘,下面就与大家分享分享,因为时间关系,[GLKeyBoard]就写的有点简单,下面就来看看具体实现
就以UITextField为例,在API中有这么一个属性inputView,当我们的UITextField成为第一响应者的时候就会弹出该view,当然我们默认情况下是系统默认的键盘,所以,要想自定义键盘,就需要在此处下功夫了。
想必写这个界面,对各位是没有什么难度的,绝对妥妥的,十来分钟就搞定的事情,贴上部分代码
-(void)initializeViewComponents
self.backgroundColor =UICOLOR_FROM_RGB_OxFF(0xbfc5ca)
NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@"千",@"万",@"十万",@"百万",@"Delete",@"清除",]
for (int i = 0
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]
button.tag = kKeyBoardTag + i
[button setBackgroundColor:UICOLOR_FROM_RGB_OxFF(0xfefefe)]
if (i == kKeyBoardNumber-2) {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
[button setBackgroundColor:UICOLOR_FROM_RGB_OxFF(0xff4238)]
[button setTitleColor:UICOLOR_FROM_RGB_OxFF(0x303030) forState:UIControlStateNormal]
if (i & 9) {
button.titleLabel.font = [UIFont systemFontOfSize:12]
button.titleLabel.font = [UIFont systemFontOfSize:15]
button.layer.cornerRadius = 5
[button.layer setMasksToBounds:YES]
[button setTitle:array[i] forState:UIControlStateNormal]
[button addTarget:self action:@selector(keyBoardClick:) forControlEvents:UIControlEventTouchUpInside]
[self addSubview:button]
if (i == 0) {
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(kKeyBoardWidth), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(@(GTReViewXFloat(kKeyBoardTopPadding)))
make.left.equalTo(self.mas_left).offset(GTReViewXFloat(5))
_lastButton = button
}else if(i & 10){
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(kKeyBoardWidth), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(@(GTReViewXFloat(kKeyBoardTopPadding)))
make.left.equalTo(_lastButton.mas_right).offset(GTReViewXFloat(kKeyBoardMiddlePadding))
_lastButton = button
}else if(i == 10){
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(kKeyBoardBigWidth), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(_lastButton.mas_bottom).offset((GTReViewXFloat(kKeyBoardTopPadding)))
make.left.equalTo(self.mas_left).offset(GTReViewXFloat(5))
_lastButton = button
}else if (i & 14 && i & 10){
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(kKeyBoardBigWidth), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(_lastButton.mas_top)
make.left.equalTo(_lastButton.mas_right).offset(GTReViewXFloat(kKeyBoardBottomMiddlePadding))
_lastButton = button
}else if (i == 14){
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(100), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(_lastButton.mas_top)
make.left.equalTo(_lastButton.mas_right).offset(GTReViewXFloat(kKeyBoardBottomMiddlePadding))
_lastButton = button
}else if (i == 15){
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(GTReViewXFloat(84), GTReViewXFloat(kKeyBoardHeight)))
make.top.equalTo(_lastButton.mas_top)
make.left.equalTo(_lastButton.mas_right).offset(GTReViewXFloat(kKeyBoardBottomMiddlePadding))
_lastButton = button
这里不建议像我这样,把需要的键值写在这里,最好是新建一个plist文件,这个布局代码也有点乱,大家多担待。
2. 写完之后,激动的我赶快想看下效果,于是飞快的写下了下面的代码
GLKeyBoard *keyBoard = [[GLKeyBoard alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, GTReViewXFloat(85))]
_textFiled = [[UITextField alloc] init]
_textFiled.delegate = (id)self
_textFiled.textColor = [UIColor orangeColor]
_textFiled.borderStyle = UITextBorderStyleRoundedRect
_textFiled.placeholder = @"随便输..."
_textFiled.inputView = keyBoard
[self.view addSubview:_textFiled]
正当我兴奋不已的时候,我艹,什么鬼,点了按钮都没反应,恍然大悟,原来按钮点击那里什么都没处理….
3. 进入按钮点击后的核心处理
首先,我们的在GLKeyBoard中添加点东西
typedef NS_ENUM(NSInteger,GLKeyBoardType) {
GLKeyBoardClearAll,
GLKeyBoardDelete,
GLKeyBoardOther
typedef void(^KeyBoardClickBlcok)(GLKeyBoardType keyBoardType, NSString *text);
这样后,我们在点击处就可以添加下面的代码了
-(void)keyBoardClick:(UIButton *)sender
NSInteger tag = sender.tag - kKeyBoardT
switch (tag) {
case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:
if (self.keyBoardClickBlock) {
self.keyBoardClickBlock(GLKeyBoardOther, sender.currentTitle);
if (self.keyBoardClickBlock) {
self.keyBoardClickBlock(GLKeyBoardDelete, sender.currentTitle);
if (self.keyBoardClickBlock) {
self.keyBoardClickBlock(GLKeyBoardClearAll, sender.currentTitle);
然后我们就可以在block中对textField中进行值的处理了
一开始,我想的是在viewcontroller中,直接添加处理方法,根据按钮的类型,来改变值,但是这样的话,岂不是每个用到的地方都要写一篇,难免太复杂,于是我想到了category,这就有了UITextField+GLKeyBoard这个类的诞生
在该类中主要添加了一个方法
- (void)updateText:(NSString *)text,根据按钮点击的文字来更改输入控件的值,方法过程也不是很复杂,每个地方都有标注,相信大家看了都会明白
最主要的思想就是根据光标来分割两边的字符,然后将新的字符插入到光标位置,并移动光标的位置。
- (void)updateText:(NSString *)text
if ([text isEqualToString:@"千"]) {
[self changeTextWithNumber:1000];
}else if ([text isEqualToString:@"万"]){
[self changeTextWithNumber:10000];
}else if ([text isEqualToString:@"十万"]){
[self changeTextWithNumber:100000];
}else if ([text isEqualToString:@"百万"]){
[self changeTextWithNumber:1000000];
UITextPosition *beginning = self.beginningOfDocument;
UITextPosition *startPosition = self.selectedTextRange.start;
UITextPosition *endPosition = self.selectedTextRange.end;
NSInteger startIndex = [self offsetFromPosition:beginning toPosition:startPosition];
NSInteger endIndex = [self offsetFromPosition:beginning toPosition:endPosition];
NSString *originText = self.text;
NSString *beforeString = [originText substringToIndex:startIndex];
NSString *afterString = [originText substringFromIndex:endIndex];
if (![text isEqualToString:@""]) {
offset = text.length;
if (startIndex == endIndex) {
if (startIndex == 0) {
offset = -1;
beforeString = [beforeString substringToIndex:(beforeString.length - 1)];
offset = 0;
NSString *newText = [NSString stringWithFormat:@"%@%@%@", beforeString, text, afterString];
self.text = newT
UITextPosition *nowPosition = [self positionFromPosition:startPosition offset:offset];
UITextRange *range = [self textRangeFromPosition:nowPosition toPosition:nowPosition];
self.selectedTextRange =
到此为止,我们的键盘就OK啦,来看看效果
嗯哼,怎么感觉不对呢?
1. 位置不对
2. 键盘上面怎么多了一截呢?
排查后,才发现是导入了IQKeyboard这个类,导致键盘默认会有个工具栏在上面,但是我们美工妹子说了,不能要,好吧,于是我查看API,发现了这个参数
Automatic add IQToolbar functionality. Default is YES.
@property(nonatomic, assign, getter = isEnableAutoToolbar) BOOL enableAutoT
于是我添加了一行代码,在vc中
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
瞬间就对了,那么第一个问题怎么办呢?我首先想到的就是在键盘弹出的时候去修改坐标,但是我们必须的拿到这个键盘的view,我显示把GLKeyBoard *keyBoard这个对象当作键盘来设置,然后当头一棒,不行的,完全没反应。
看来只能自己写个方法来查找我们自定义的keyBoard了
- (UIView *)findKeyboard
UIView *keyboardView = nil;
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in [windows reverseObjectEnumerator])
keyboardView = [self findKeyboardInView:window];
if (keyboardView)
return keyboardV
return nil;
- (UIView *)findKeyboardInView:(UIView *)view
for (UIView *subView in [view subviews])
NSLog(@" 打印信息:%s",object_getClassName(subView));
if (strstr(object_getClassName(subView), "UIInputSetHostView"))
return subV
UIView *tempView = [self findKeyboardInView:subView];
if (tempView)
return tempV
return nil;
这里有个问题要注意下,就是如果你不晓得哪个是我们的keyBoard,可以在NSLog(@" 打印信息:%s",object_getClassName(subView));这个位置打个断点,看看view具体展示的是那个
网上有这样的
if (strstr(object_getClassName(subView), "UIKeyboard")),经测试是不行的,我估计应该是在后面的版本中发生了变化
在找到keyBoard这个view后,一切就变的简单了
我们只需要在键盘即将弹出的通知处添加下面代码就搞定啦
-(void) keyboadWillShow:(NSNotification *)note{
NSDictionary *info = [note userInfo];
CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame.origin.y = CGRectGetMaxY(_textFiled.frame) + 4.5;
UIView *keyBoardView = [self findKeyboard];
[UIView animateWithDuration:0.2 animations:^{
[keyBoardView setFrame:keyboardFrame];
在这里,还遇到个小坑,就是如果textField是写在cell中的,那么添加通知,最好不要在cell的初始化方法中添加,建议在textField的代理textFieldDidBeginEditing中添加,因为通知是一对多的,如果在cell的初始化方法中添加,当有很多cell的时候,键盘会上下跳动,而且坐标不是你想要的坐标
项目文件截图:
关于自定义键盘,就差不多这么,希望大家能喜欢,要放假了,祝大家端午快乐~回家包包子去了….iOS 自定义键盘
代码地址如下:
注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权
iOS系统键盘和自定义键盘的切换
注意 iOS系统键盘的九宫格键盘输入的内容
iOS开发之自定义键盘(数字,字母类型等随意切换)
iOS自定义键盘
Objective-C-如何自定义键盘(iOS)
iOS开发:自定义数字键盘(两种方式)
iOS textField弹出自定义键盘(日期时间、省市、国家)
深入讲解iOS键盘三:自定义键盘的两种方法
iOS中如何自定制键盘呢?
没有更多推荐了,自定义ios键盘
查看次数:4662
下载次数:565
上传时间:
大小:33 B
昨天刚到cocoachina上有人投稿了一篇关于自定义键盘的文章,于是下下载下来了,但是感觉写的很繁杂,于是自己写了一个简单的自定义ios键盘的demo.
您还没有登录!请或
下载过该代码的还下载了
本周热门下载
&2018 Chukong Technologies,Inc.
京公网安备89iOS键盘开发之自定义键盘的两种方法_达内iOS培训
400-111-8989
iOS键盘开发之自定义键盘的两种方法
时间: 16:29
iOS系统提供了多种键盘,我们可以通过Enum类型设置。但有的时候由于某些特殊业务的需要,我们不得不自定义键盘,比如某些银行的APP处于安全考虑,他们键盘数字的位置是随机的,这个时候只能自定义键盘。幸运的是,iOS也为我们提供了多种方式自定义键盘。我们可以根据自身情况选择合适的方案。
typedef&NS_ENUM(NSInteger,&UIKeyboardType)&{
&&&&UIKeyboardTypeDefault,
&&&&UIKeyboardTypeASCIICapable,&can&enter&ASCII&characters
&&&&UIKeyboardTypeNumbersAndPunctuation,
&&&&UIKeyboardTypeURL,
&&&&UIKeyboardTypeNumberPad,
&&&&UIKeyboardTypePhonePad,
&&&&UIKeyboardTypeNamePhonePad,
&&&&UIKeyboardTypeEmailAddress,
&&&&UIKeyboardTypeDecimalPad&,
&&&&UIKeyboardTypeTwitter&,
&&&&UIKeyboardTypeWebSearch&,
&&&&UIKeyboardTypeASCIICapableNumberPad&,
&&&&UIKeyboardTypeAlphabet&=&UIKeyboardTypeASCIICapable,
对现有键盘稍加改动
这种情况适合身份证的输入。由于身份证大部分情况下都是数字,偶尔可能出现“X”字符,如果我们弃用数字键盘直接使用诸如UIKeyboardTypeNumbersAndPunctuation等包含数字和字符的键盘又显得没有太大必要,那稍加改动数字键盘是个不错的选择。思路也很简单,在弹出键盘的同时获取键盘对应的window,在window上加上我们需要的按钮即可。如下是笔者改动的数字键盘。
下面,我对重点代码做个讲解:
-(void)keyboardWillShow:(NSNotification&*)notification{
&&&&//移除掉原先添加的按钮
&&&&[self.extrakeyButton&removeFromSuperview];
&&&&self.extrakeyButton&&&&&=&
&&&&//这几行代码相信看了之前系列博客的读者应该很熟悉了
&&&&NSDictionary&*userInfo&&=&[notification&userInfo];
&&&&CGFloat&animationDuration&&&=&[[userInfo&objectForKey:
UIKeyboardAnimationDurationUserInfoKey]&floatValue];
&&&&CGRect&kbEndFrame&&&&&&&&&&&=&
[userInfo[UIKeyboardFrameEndUserInfoKey]&CGRectValue];
&&&&CGFloat&kbHeight&&&&&&&&&&&&=&
kbEndFrame.size.
&&&&//以下是对添加的X按钮Frame的设置
&&&&CGFloat&extrakeyButtonX&=&0;
&&&&CGFloat&extrakeyButtonW&=&0;
&&&&CGFloat&extrakeyButtonH&=&0;
&&&&extrakeyButtonW&=&(SCREEN_WIDTH&-&7)&/&3;
&&&&extrakeyButtonH&=&kbHeight&/&4;
&&&&CGFloat&extrakeyButtonY&=&0;
&&&&extrakeyButtonY&=&SCREEN_HEIGHT&+&kbHeight&-&
extrakeyButtonH;
&&&&//创建“X”按钮,并设置相应的属性
&&&&self.extrakeyButton&=&[[UIButton&alloc]&initWithFrame:
CGRectMake(extrakeyButtonX,&extrakeyButtonY,&
extrakeyButtonW,&extrakeyButtonH)];
&&&&[self.extrakeyButton&addTarget:self&action:
@selector(buttonDidClicked)&forControlEvents:
UIControlEventTouchUpInside];
&&&&self.extrakeyButton.titleLabel.font&=&[UIFont&
systemFontOfSize:27];
&&&&[self.extrakeyButton&setTitle:@"X"&forState:
(UIControlStateNormal)];
&&&&[self.extrakeyButton&setTitleColor:[UIColor&
blackColor]&forState:UIControlStateNormal];
&&&&//获取键盘对应的Window(这段代码只在iOS&11上做过测试,还不够严谨)
&&&&UIWindow&*tempWindow&=&[[[UIApplication&
sharedApplication]&windows]&lastObject];
&&&&[tempWindow&addSubview:self.extrakeyButton];
&&&&//设置动画
&&&&[UIView&animateWithDuration:animationDuration&
animations:^{
&&&&&&&&CGRect&frame&&&&=&self.extrakeyButton.
&&&&&&&&frame.origin.y&&=&frame.origin.y&-&kbH
&&&&&&&&self.extrakeyButton.frame&=&
&&&&}&completion:nil];
以上代码只在iOS11的iPhone
8 Plus 上做过测试,可能不具备一定的普遍性,比如iPhone
X的键盘位置有一定改变,按钮的位置需要加代码兼容。因此这种解决方案非常局限,只能用于身份证等业务非常简单的备选方案。有个者对这个键盘做了个封装,读者可以点击这里获取
自己设计一个键盘
iOS中可以通过设置TextField/TextView的inputView来定制键盘,
//The&custom&input&view&to&display&when&the&text&field&
becomes&the&first&responder.
@property(readwrite,&strong)&UIView&*inputV
这个自定义键盘就解决了我们开头提到的问题:键盘上的数字是随机排列的0-9,如果需要添加新的按键可以添加到键盘剩下的空白中。下面来讲解一下核心代码的实现:
CustomKeyboardView&*keyView&=&[[CustomKeyboardView&alloc]&
initWithFrame:CGRectMake(0,&0,&SCREEN_WIDTH,&176)];
self.xTextField.inputView&=&keyV
-&(instancetype)initWithFrame:(CGRect)frame
&&&&self&=&[super&initWithFrame:frame];
&&&&if&(self)&{
&&&&&&&&self.backgroundColor&=&UIColor.lightGrayC
&&&&&&&&//创建数字数组,并打乱
&&&&&&&&NSMutableArray&*integers&=&[[NSMutableArray&alloc]
&&&&&&&&for&(NSInteger&i&=&0;&i&&&10;&++i)&{
&&&&&&&&&&&&[integers&addObject:@(i)];
&&&&&&&&NSArray&&*shuffledIntegers&=&[self&shuffle:
&integers];
&&&&&&&&//添加数字按钮
&&&&&&&&for&(NSInteger&index&=&0;&index&&&10;&++index)&{
&&&&&&&&&&&&UIButton&*btn&=&[UIButton&buttonWithType:
&UIButtonTypeCustom];
&&&&&&&&&&&&btn.backgroundColor&=&UIColor.grayColor&;
&&&&&&&&&&&&[btn&addTarget:self&action:@selector
&(buttonDidClicked:)&forControlEvents:UIControl
&EventTouchUpInside];
&&&&&&&&&&&&btn.frame&=&CGRectMake(CGRectGetWidth
&(self.frame)&/&3&*&(index&%&3&),&45&*&&(index&/&3&)&,&
&CGRectGetWidth(self.frame)&/&3&-&1,&44);
&&&&&&&&&&&&NSString&*indexString&=&[NSString&
&stringWithFormat:@"%@",shuffledIntegers[index]];
&&&&&&&&&&&&[btn&setTitle:indexString&forState:UIControl
&StateNormal];
&&&&&&&&&&&&[self&addSubview:btn];
&&&&return&
打乱键盘中数字的布局方法很多,一般的洗牌算法就可以实现:
-(NSArray&*)shuffle:(NSArray&*)array
&&&&if(array&==&nil&||&array.count&&&1)
&&&&&&&&return&
&&&&NSMutableArray&*resultArray&=&[NSMutableArray&
&arrayWithArray:array];
&&&&NSInteger&
&&&&NSNumber&*
&&&&for(NSInteger&index&=&0;&index&&&array.&index&++
&&&&&&&&value&=&rand()&%&resultArray.
&&&&&&&&median&=&resultArray[index];
&&&&&&&&resultArray[index]&=&resultArray[value];
&&&&&&&&resultArray[value]&=&
&&&&return&resultA
最后是点击键盘按钮后的回调处理
-(void)buttonDidClicked:(UIButton&*)&sender{
&&&&if&(self.delegate)&{
&&&&&&&&[self.delegate&keyboardItemDidClicked:sender.
titleLabel.text];
相比较而言,大家是否觉得这种方法比第一种方法既简单又简洁?
在IOS开发过程中我们总会遇到各种各样的小问题,有些IOS开发小问题并不是十分容易解决。在此达内小编就总结一下,在开发中遇到的各种小问题,以及解决方法。比较普遍的就不再提了,这里主要讲一些你可能不知道的。
在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题。通常这种问题发生的原因就是对于 view 何时真正更新的错误理解。想
我们在很多项目中大量使用了webview加载H5,考虑到WKWebView的性能优于UIWebView,所以就选择了WKWebView。WKWebView在使用的过程中,有很多内容值得我们去记录和研究,在今天的ios培训中我们就将WKWebView在实际开发中的使用汇总,分享给大家。
好比你在网页上放了一个Button,如果用HTML你就可以设置他的摆放位置,在哪哪个控件里.但是你不可以设置大小,颜色,圆角之类的属性.
Copyright (C)
Tedu.cn All Rights Reserved 京ICP备号-56 达内时代科技集团有限公司 版权所有
选择城市和中心
达内北京亦庄大学生实训基地
达内北京网络营销中心
达内北京会计中心拒绝访问 | www.wangchao.net.cn | 百度云加速
请打开cookies.
此网站 (www.wangchao.net.cn) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(43fb838be0374d14-ua98).
重新安装浏览器,或使用别的浏览器}

我要回帖

更多关于 iOS安全攻防-键盘安全6 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信