gpt4 book ai didi

iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

最近做一个搜索用户的功能,这里使用了uisearchbar。由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做:

self.searchbar.keyboardtype = uikeyboardtypenumberpad;如果使用的不是搜索框而是textfield输入框,可以设置textfield的键盘属性来展示 self.textfield.keyboardtype = uikeyboardtypenumberpad;监听事件如下所示即可.

iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

但是这里有个问题,就是数字键盘上面没有“搜索”按钮,这样子用户在输入完手机号码后无法搜索。所以这个时候我们需要自己添加一个自定义的搜索按钮,然后加到键盘上面.

解决思路如下所示:

1.自定义搜索button 。

2.监听键盘出现的事件 。

3.遍历搜索的windows窗体,找到键盘的窗体,然后遍历其子视图,找到我们真正需要的键盘视图 。

4.把我们自定义的按钮加到上面找到的视图里 。

这里要注意的一点,随着ios sdk的不断发展,keyboard的视图名称也不断在更新变化,当你调试以下代码无法得到期待的效果时,请重新遍历一次窗台,然后慢慢调试,找到真正需要的视图名称.

解决代码 。

1.自定义搜索按钮 。

?
1
2
3
4
5
6
// 搜索按钮
_searchbutton = [uibutton buttonwithtype:uibuttontypecustom];
_searchbutton.frame = cgrectmake(0, 163, 106, 53);
[_searchbutton settitle:@ "搜索" forstate:uicontrolstatenormal];
[_searchbutton settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
[_searchbutton addtarget:self action:@selector(searchbuttondidtouch:) forcontrolevents:uicontroleventtouchupinside];

2.监听键盘出现的事件 。

?
1
2
3
4
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshowondelay:) name:uikeyboardwillshownotification object:nil];
- ( void )keyboardwillshowondelay:(nsnotification *)notification {
[self performselector:@selector(keyboardwillshow:) withobject:nil afterdelay:0];
}

这里面监听通知后的执行函数并非立马执行查找窗体的函数,是因为在ios4后,键盘添加到窗体的事件放到了下一个eventloop,所以我们采用了延迟的方法.

3. 遍历视图,并添加按钮 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- ( 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 (__strong uiview *possiblekeyboard in [keyboardwindow subviews]) {
if ([[possiblekeyboard description] hasprefix:@ "<uiinputsetcontainerview" ]) {
for (__strong uiview *possiblekeyboard_2 in possiblekeyboard.subviews) {
if ([possiblekeyboard_2.description hasprefix:@ "<uiinputsethostview" ]) {
foundkeyboard = possiblekeyboard_2;
}
}
}
}
if (foundkeyboard) {
if ([[foundkeyboard subviews] indexofobject:_searchbutton] == nsnotfound) {
[foundkeyboard addsubview:_searchbutton];
} else {
[foundkeyboard bringsubviewtofront:_searchbutton];
}
}
}

iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

以上所述是小编给大家介绍的ios开发之uikeyboardtypenumberpad数字键盘自定义按键,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。

原文链接:http://www.cnblogs.com/MasterPeng/archive/2016/08/24/5802340.html 。

最后此篇关于iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键的文章就讲到这里了,如果你想了解更多关于iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com