对iOS系统键盘的hack以及注意事项
来源:互联网 发布:时间机器字幕制作软件 编辑:程序博客网 时间:2024/05/01 20:44
原文转载自:http://hikui.is-programmer.com/posts/35019.html
对iOS系统键盘的hack以及注意事项
在做东方财富通新版的时候,需求组给出的新的需求是,数字键盘使用我们自己做的,而字母键盘使用系统自带的键盘,然而这里出现的一个问题就是,系统键盘进行输入法切换的时候是没有办法切换到自带的键盘中去的。这时候就需要对系统自带键盘进行hack。
原理很简单,就是在系统键盘弹出来时找到系统键盘所在的UIView,然后贴上自己的按钮覆盖掉原先的按钮。难点在于如何找到系统键盘所在的View。
效果图:
从效果图上可见,原先系统键盘的123和地球的图标被替换成了自定义的按钮。
做法:首先在当前的ViewController中监听:UIKeyboardDidShowNotification
[[
NSNotificationCenter
defaultCenter]addObserver:
self
selector:
@selector
(keyboardDidShow:) name:UIKeyboardDidShowNotification object:
nil
];
然后自己实现keyboardDidShow方法,在里面根据需要调用下面hackSystemKeyboard方法:
- (
void
)hackSystemKeyboard
{
UIView *foundKeyboard = [
self
getSystemKeyboard];
if
(foundKeyboard) {
//如果有原先存在的hack button,删之
[
self
clearKeyboardHack];
//hackButton 为123button
//新建hackButton,根据orentation定义不同的frame
UIButton *hackButton = [UIButton buttonWithType:UIButtonTypeCustom];
hackButton.backgroundColor = [UIColor clearColor];
hackButton.tag = 19100;
if
(
self
.isLandscape) {
hackButton.frame = CGRectMake(3, 124, 90, 38);
UIImage *btnBG = [UIImage imageNamed:@
"hackButtonLandscape.png"
];
[hackButton setBackgroundImage:btnBG forState:UIControlStateNormal];
}
else
{
hackButton.frame = CGRectMake(2, 173, 77, 43);
UIImage *btnBG = [UIImage imageNamed:@
"hackButton.png"
];
[hackButton setBackgroundImage:btnBG forState:UIControlStateNormal];
}
[hackButton setShowsTouchWhenHighlighted:
YES
];
[hackButton addTarget:
self
action:
@selector
(switchToNumberPad:) forControlEvents:UIControlEventTouchUpInside];
[foundKeyboard addSubview:hackButton];
}
}
这里要注意的是,如果这个ViewController支持横屏,则需要准备两套button图片。并且在willAnimateRotationToInterfaceOrientation中需要调用hackSystemButton这个方法。
里面比较重要的是getSystemKeyboard方法:
- (UIView *)getSystemKeyboard
{
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
nil
;
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
;
}
}
}
return
foundKeyboard;
}
这里利用了iOS系统组件的Description进行View的判断,iOS 4以后的版本和之前的版本不一样,新版本中把UIKeyboard放在UIPeripheralHostView中。
最后需要注意的是,一个系统键盘在一个应用程序里面是共享的,所以如果这个ViewController中对键盘进行了hack,而在另外一个ViewController中不需要hack,那么需要在本ViewController退出的时候把Hack清理掉。但是在ViewWillDisappear中,我发现getSystemKeyboard方法寻找到的View并不是真正的keyboard,我的一个变通的办法就是在所有会导致ViewWillDisappear的步骤中插入clearKeyboardHack方法。
101年10月17日更新:
清理hack的时机应该是键盘将要隐藏前。只要在NotificationCenter中注册名为UIKeyboardWillHideNotification,收到notification的时候进行一次清理即可。
- 对iOS系统键盘的hack以及注意事项
- Hack the UIKeyboard(自定义iOS内置的键盘)
- iOS 系统 键盘的通知
- ios之让输入键盘隐藏消失的方法/以及系统通知的实现
- ios上面对输入键盘的处理
- iOS中对键盘的处理
- iOS 定时器的使用以及注意事项
- ios本地化以及注意事项
- iOS中键盘样式设置、隐藏系统键盘的方法
- iOS系统键盘和自定义键盘的切换
- iOS获取系统键盘的高度
- iOS如何获取系统键盘的高度
- 防止验证码防被hack的注意事项
- iOS系统键盘样式
- iOS UITextView 限制字数(解决了截断输入联想和对系统键盘中文不友好的问题)
- iOS-程序的启动过程以及相关的注意事项
- 关于IOS键盘遮住输入框以及关闭的方法。
- ios -- 虚拟键盘弹出挡住textfield的分析以及解决办法
- 利用WNet系列函数枚举和映射网络资源
- struts2 spring集成时struts.xml文件中action的class属性值意义
- C或C++中函数的参数传递包括:值传递、指针传递、引用传递
- linux入门
- 在eclipse中将android项目生成apk并且给apk签名
- 对iOS系统键盘的hack以及注意事项
- JS中使用Ajax获取的数据赋值不了问题的解决方案
- poj 2253 Frogger(dijkstra)
- java心得
- #define命令的一些高级用法
- Ffmpeg和SDL创建线程(转)
- JS错误:SCRIPT1028:缺少标识符、字符串或数字
- Velocity语法强化4之语义问题
- O(n lg n)-time Algorithm to Find the Longest Monotonically Increasing Sub-sequence