为UIKeyboardTypeNumberPad增加自定义按键

在 iPhone 上面開發應用程式時, 在使用輸入鍵盤時, 或多或少都會遇到客制化鍵盤的問題, 這邊筆者以
簡單的數字鍵盤來示範客制化的動作. 這部份我想網路上已經有不少 sample code , 但大部份基本上都是
以 SDK 3.x 的版本去實作, 以"特定寫法"來實作客制化在 iOS4 會有問題, 這部份稍候會提到兩版本的差異.

上述看到的例子是 UIKeyboardTypeNumberPad 搭配 "Done" 的圖示所組合而成的. 在開
始介紹如何實作之前, 先稍微提一下網路上查到的一些範例寫法. 因為 SDK 升版之
後在架構上有做了些修改, 所以導致行為上的不正確. 以下面這例子為例, 它可以正
常的在 iOS4 之前的版本運行, 但在 iOS4 上卻會有看不到上面 "Done" 圖示的問題.

- (void)loadView{
...
textFieldContent.delegate= self;
textFieldContent.placeholder= @"press me";
textFieldContent.keyboardType= UIKeyboardTypeNumberPad;
textFieldContent.returnKeyType= UIReturnKeyDone;
[self.viewaddSubview:textFieldContent];
[textFieldContentrelease];
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWillShow:) 
name:UIKeyboardWillShowNotification
object:nil];
}

- (void)keyboardWillShowOnDelay:(NSNotification*)notification{
UIButton*doneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
doneButton.frame= CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted= NO;
[doneButton setImage:[UIImageimageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImageimageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:selfaction:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIWindow* tempWindow = [[[UIApplicationsharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(inti=0; i<[tempWindow.subviewscount]; i++) {
keyboard = [tempWindow.subviewsobjectAtIndex:i];
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}


上述這段代碼主要原理是透過跟 OS 註冊 keyboard 相關的 notification, 並在顯示
keyboard 時, 在 keyboard view 上添加所需要的特定 UIView, 簡單流程大致如下
1. 註冊 UIKeyboardWillShowNotification : 當 keyboard 要秀時, OS 就會呼叫
    keyboardWillShow
2.  當被 keyboardWillShow 叫用時, 搜尋屬於 keyboard 的 view
   if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
3. 當找到所需要的 view 時, 再將需要的 view 加入即可
   [keyboard addSubview:doneButton];
上面就是一個 customized keyboard 的簡單實作流程. 但是為什麼這段 code 會無法
在 iOS4 上正確執行呢? 問題點主要出在上述的第 2 個步驟.
在舊的 SDK 中, 當 UIKeyboardWillShowNotification 事件發生且叫用 keyboardWillShow 
時, 此時的 keyboard view 已經被添加到 windows 裡了, 但是在 iOS4 的世界中, 相同
情況發生時, keyboard view 卻會在下個 event loop 裡才會被添加到 windows 中, 也
就是因為如此, 所以上述
[[[UIApplication sharedApplication] windows] objectAtIndex:1];
會找不到 keyboard view. 除了這原因以外, 還有另一個重要的差異性, 第 2 步驟所比
對的 @"<UIKeyboard" 字串在 iOS4 中也被修正過, 它被藏在 @"<UIPeripheralHostView"

裡.

針對這兩點, 所以將只要將之修正即可正常的在 iOS4 上執行
1. keyboard view
   既然知道是 keyboard view 會在下個 event loop 才會被放到 windows 裡, 所以我們
   可以透過下面方式將 keyboardWillShow 延遲叫用
   [self performSelector:@selector(keyboardWillShow:) withObject:nil afterDelay:0];
2. 修正比對 @"<UIKeyboard" 的方式

if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) 
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"])  
{
foundKeyboard = possibleKeyboard;
break;
}                
經過上述兩個修正之後的 code 大概會如下 :
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWillShowOnDelay:) 
name:UIKeyboardWillShowNotification 
object:nil];
- (void)keyboardWillShowOnDelay:(NSNotification *)notification
{
[selfperformSelector:@selector(keyboardWillShow:) withObject:nilafterDelay:0]; 
}
- (void)keyboardWillShow:(NSNotification *)notification
{    
UIView *foundKeyboard = nil;
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) 
{
if (![[testWindow class] isEqual:[UIWindow class]]) 
{
keyboardWindow = testWindow;
break;
}
}
if (!keyboardWindow) return;
for (UIView *possibleKeyboard in [keyboardWindow subviews]) 
{            
//iOS3
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) 
{
foundKeyboard = possibleKeyboard;
break;
}
else
{
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) 
{
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}                                                                                
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) 
{
foundKeyboard = possibleKeyboard;
break;
}                
}            
}
if (foundKeyboard) 
{
// create custom button
UIButton*doneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted= NO;
[doneButton setImage:[UIImageimageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImageimageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:selfaction:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
[foundKeyboard addSubview:doneButton];
}
}

 

 

 

 

 

 

If you have ever written an iPhone app that requires numeric input,then you surely know about the UIKeyboardTypeNumberPad. And if you haveever used that flavor of the iPhone's keyboard, then you surely knowthat it lacks one very important feature: The UIKeyboardTypeNumberPaddoes not have a "return" key.
In fact every other keyboard type (except for the pretty similarUIKeyboardTypePhonePad) does offer the possibility to be dismissed bysetting the returnKeyType property of the correspondingUITextInputTraits implementor. So how does one achieve the same effectwith the number pad? We have found a workround!
When looking at the number pad, you'll notice that there is anunused space on its bottom left. That's where we are going to plug inour custom "return" key.


To make it short: take a screenshot, cut out the whole backspacekey, flip it horizotally, clear its backspace symbol in Photoshop andoverlay it with the text that we want on our “return” key. We’ve chosento label it “DONE”. Now we have the image for our custombutton’s UIControlStateNormal. Repeat the whole procedure (with atouched backspace key when taking the screenshot) to get a second imagefor our button’s UIControlStateHighlighted. Here’s the result:
   

Now back to coding. First we need to know when the number pad isgoing to be slided up on the screen so we can plug in our custom buttonbefore that happens. Luckily there’s a notification for exactly thatpurpose, and registering for it is as easy as:
 
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];
 [/pre]Don't forget to remove the observer from the notification center in the appropriate place once you're done with the whole thing:
 
[[NSNotificationCenter defaultCenter] removeObserver:self];[/pre]Now we’re getting to the heart of it. All we have to do in thekeyboardWillShow method is to locate the keyboard view and add ourbutton to it. The keyboard view is part of a second UIWindow of ourapplication as others have already figured out (see this thread).So we take a reference to that window (it will be the second window inmost cases, so objectAtIndex:1 in the code below is fine), traverse itsview hierarchy until we find the keyboard and add the button to itslower left:
 
- (void)keyboardWillShow:(NSNotification *)note {  
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
 
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
}[/pre]Voilà, that’s it! The empty space for our button starts atcoordinate (0, 163) and has the dimensions (106, 53). The doneButtonmethod has to be written now of course, but that’s not hard any more.Just make sure that you call resignFirstResponder on the text fieldthat is being edited to have the keyboard slide down.

 

 

 

 

有一种思路叫寄生...

我相信是懒人推动了世界的发展,既然iphone有了自己的软件盘,我们什么还要自己实现其功能呢。
so,只要寄生在上面就行了。

感谢alan转载的文章给的灵感。
http://www.cocoachina.com/bbs/read.php?tid-3999.html

思路:
1.用静态方法找到应用程序当前view(window)中的UIKeyboard的view
2.在键盘的view上帖上自己的view,(精彩了,这个自己的view就是你自己键盘,任意发挥,什么类型键盘都可以做了)
3.根据需要调整系统键盘的大小以满足你想要的尺寸
4.给自己的键盘view上的button添加方法,实现功能

主要代码:
添加自身类为键盘事件的观察者

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];

核心思路代码:
- (void)keyboardWillShow:(NSNotification *)note
{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//知识点
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard setFrame:CGRectMake(0, 460, 320, 345)];
[self congfigKeypad];

[keyboard addSubview:keyPadView1];

}
}
}

比如配置方法可以是这样:
-(void)congfigKeypad
{
SearBtn *one = [[SearBtn alloc] initWithFrame:CGRectMake(81, 3, kNumPadW, kNumPadH) index:1 ContextString:@"1" type:kNumPadType];
[one setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[one addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
//......略
}

添加NSMutalbeString作为文本域字串的容器,点击button后append的button对应的字串。
- (void)buttonClickAtIndex:(id)sender
{
SearBtn *btnItem = (SearBtn*)sender;
NSString *str = btnItem->btnText;
[s_text appendString:str];
[sBar setText:s_text];
}


再实现一个deleteChar的方法作为退格键
思路:
if ([s_text length] > 0)
{
NSRange rang;
rang.location = [s_text length] - 1;
rang.length = 1;
[s_text deleteCharactersInRange:rang];
}


现在点击各种文本域,应该就可以现实自己的键盘了。

继续优化
用textfield的代理方法控制键盘的字串类型,长度,和响应消失
[ 此帖被evangel在2009-12-17 22:32重新编辑 ]
图片:图片 1.png 

图片:图片 2.png 

  

Q:如果是通过[lang=objc][/lang]来显示代码的话,就不能再编辑了,不然都是乱码……
A :也可以,要先把代码去掉提交一次,然后再加上代码修改再提交一次……

隐藏键盘

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            //[keyboard addSubview:keyboardView];
            keyboard.hidden=YES;
    }

还能隐藏键盘,使用自己的view载入动画和控制位置,也不需要担心背景是否会显示出默认键盘!!lucky!!

 
多个页面应该不会有问题,切换页面时候键盘就消失了。

同一个页面的时候:
1。设置键的view为实例变量

2。给有自定义键盘的那个textfield添加delegate方法:- (void)textFieldDidEndEditing:(UITextField *)tf;
在这个方法里[myKeyPad removeFromSuperview];

3。在- (void)keyboardWillShow:(NSNotification *)note;里判断有自定键盘的textfield为firstResponder时候,再add键盘的view。
if([textField isFirstResponder])  [myKeyPad addSubview:doneButton];

当然还有很多别的方法

 

重新回复一下这个帖子,因为时间过去大半年了,我用SDK4.0,无法按照楼主的代码直接实现,而是做了一点点的修改才能实现。大家谁有时间帮忙用最新SDK测试一下,看看是不是如此。

问题与修改1
原帖中用UIKeyboardWillShowNotification,tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1] 的subview数目为0!
我改UIKeyboardDidShowNotification,一切正常。

问题与修改2
原帖中用了字符串@"<UIKeyboard"来检测键盘的view,在我这不管用。
我改用了@"<UIPeripheralHostView"

结果:

修改后,在SDK4.0的模拟器和真机上测试ok!

 

转载于:https://www.cnblogs.com/pengyingh/articles/2418150.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/267224.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity 内建数据索引

看到多少补多少 顶点输入类型 UnityCG.cginc Line:58 struct appdata_base {float4 vertex : POSITION;float3 normal : NORMAL;float4 texcoord : TEXCOORD0;UNITY_VERTEX_INPUT_INSTANCE_ID };struct appdata_tan {float4 vertex : POSITION;float4 tangent : TANGENT;float…

车载安卓中控改鸿蒙,教你改装车子中控安卓大屏,看一遍你就会了,改完后低配秒变高配...

因为我这个车子年代比较久了&#xff0c;所以没有中控大屏&#xff0c;需要导航的时候我都是把手机放在出风口支架这个地方&#xff0c;这种磁吸的支架有时候过减速杠一震动就掉下来了&#xff0c;我又不想在上面粘什么东西&#xff0c;所以干脆就花几百块钱买了一个能够插手机…

pip 删除安装包_Python中PIP的快速指南

无论您是Python新手还是有经验的Python开发人员&#xff0c;不使用pip安装命令是不可能编写或开发Python应用程序的。在这篇文章中&#xff0c;我将对Pip进行详细介绍。Pip是Python Package Manager的缩写。当您在系统上安装python时&#xff0c;它附带了一组预定义的包&#x…

海量数据前N大

最大堆即大根堆 -- 求前n小最小堆即小根堆 -- 求前n大比如求前n小&#xff0c;对于当前元素&#xff0c;和最大堆的堆顶元素&#xff08;即堆最大值&#xff09;比较&#xff0c;如果小于堆顶元素&#xff0c;则替换堆顶元素&#xff0c;并调整堆。这样扫描一遍就可以得到最小的…

react项目在ie空白解决

先安装babel-polyfill npm install babel-polyfill --save 2.在入口文件(main.js)最上方将其import进来 import babel-polyfill // 下面是别的代码&#xff0c;不需要动 import Vue from vue import App from ./App转载于:https://www.cnblogs.com/lcosima/p/8893799.html

元素不包括_干货 | FDA法规对元素杂质的限度控制及计算方法

元素杂质又称重金属&#xff0c;重金属原义指比重大于5的金属&#xff0c;元素杂质包括可能存在于原料、辅料或制剂中&#xff0c;来源于合成中催化剂残留、药品生产制备过程中引入或辅料中存在的、生产设备引入、或容器密闭系统引入。某些元素杂质不仅对药品的稳定性、保质期产…

web 日志分析工具 awstats 简单安装

awstats安装使用awstats是一个开源的日志分析工具&#xff0c;用perl编写。感觉还不错&#xff0c;把安装记录写下&#xff0c;也许有人用的到。可以分析大部分标准日志数据&#xff0c;如apache的&#xff0c;nginx的都可以首先下载awstats工具&#xff0c;http://awstats.sou…

MyOffic(经理评分)

1 public class SE2 {3 //员工类4 private int id;5 6 public int Id7 {8 get { return id; }9 set { id value; } 10 } 11 private string name; 12 13 public string Name 14 …

android二分查找法简书,【PYTHON】二分查找算法

二分查找&#xff1a;在一段数字内&#xff0c;找到中间值&#xff0c;判断要找的值和中间值大小的比较。如果中间值大一些&#xff0c;则在中间值的左侧区域继续按照上述方式查找。如果中间值小一些&#xff0c;则在中间值的右侧区域继续按照上述方式查找。直到找到我们希望的…

win7讲述人修复_揭秘:干掉了win7!为何win10屡被吐槽它却“永世留芳”

干掉了win7系统&#xff0c;win10似是更加地“肆无忌惮”了&#xff01;自微软宣布停止Windows 7的全部支持&#xff0c;而后win7的老用户越发频繁地收到win10的升级通知。要知道&#xff0c;“停止全部支持”也就意味着win7不再有任何新特性&#xff0c;在安全方面也不再能得到…

Ubuntu修改环境变量

修改用户环境变量 用户环境变量通常被存储在下面的文件中&#xff1a; ~/.profile~/.bash_profile 或者 ~./bash_login~/.bashrc上述文件在Ubuntu 10.0以前版本不推荐使用。 系统环境变量 系统环境变量一般保存在下面的文件中&#xff1a; /etc/environment/etc/profile/etc/ba…

Javasript设计模式之链式调用

写过jquery的可能都知道&#xff0c;jquery里面可以很方便的使用以下代码&#xff1a; // 不使用链式调用 const element $(ele); element.addClass(red); element.removeClass(green); element.show();// 链式调用 $(ele).addClass(red).removeClass(green).show(); 而jquery…

实现图片打乱_2020 回顾 | 25张图片,记录潮州的这一年

一年将近&#xff0c;万物更新这一年&#xff0c;不简单&#xff0c;也不容易让我们把时针拨回到1月通过镜头来回顾潮州这一年来令人难忘、激动和高光的时刻// 1月 //- January - 1月份的潮州充满了年味大家都沉浸在迎接新年的喜悦心情中期盼着新年的到来突如其来的疫情打乱了所…

html5 百度地图api文档,开发指南--百度地图JavaScript API大众版.doc

开发指南--百度地图JavaScriptAPI大众版开发指南--百度地图JavaScriptAPI大众版简介 JavaScript API大众版JavaScript API功能介绍百度地图JavaScript API是一套由JavaScript语言编写的应用程序接口&#xff0c;它能够帮助您在网站中构建功能丰富、交互性强的地图应用&#xff…

如何导出SQL Azure脚本?

1). Tasks—Generate Scripts 2). In the “Generate and Publish Scripts” dialog, Next -- Next 3).Click “Advanced”, set likes following: 转载于:https://www.cnblogs.com/RobotTech/archive/2012/03/28/2420707.html

迭代器以及迭代器的作用

#迭代器迭代是访问集合元素的一种方式,迭代器是一个能够记住遍历位置的对象 迭代器对象从集合的第一个元素开始访问,直到所有的元素都被访问完结束 如果想访问一个元素,需要把这个元素前面的所有元素都遍历后,才可以访问#1.可迭代的对象,像(1),(2)这些对象称之为可迭代对象(Ite…

jh锂电保护电路_一文弄懂,锂电池的充电电路,以及它的保护电路方案设计

锂电池特性首先&#xff0c;芯片哥问一句简单的问题&#xff0c;为什么很多电池都是锂电池&#xff1f;锂电池&#xff0c;工程师对它都不会感到陌生。在电子产品项目开发的过程中&#xff0c;尤其是遇到电池供电的类别项目&#xff0c;工程师就会和锂电池打交道。这是因为锂电…

ImageView相关

ImageViewActivity.java package src.com; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView; public class ImageViewActivity extends Activity { /* 声明 Button、ImageView对…

html怎么设置闪烁字,HTML最简单的文字闪烁代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼Titlekeyframes blink{0%{opacity: 1;}50%{opacity: 1;}50.01%{opacity: 0;}100%{opacity: 0;}}-webkit-keyframes blink {0% { opacity: 1; }50% { opacity: 1; }50.01% { opacity: 0; }100% { opacity: 0; }}-moz-keyframes blin…

as上的git到码云操作_如何使用git从码云克隆项目到本地?

https://www.cnblogs.com/gbb123/p/6784822.html前段时间&#xff0c;有读者微信问我&#xff0c;如果使用Git从码云或者Github 克隆代码到本地&#xff0c;然后进行提交代码的操作 。可能是新手&#xff0c;刚接触编程&#xff0c;我就从网上找到了这篇文章给他看&#xff0c;…