国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS開(kāi)發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤(pán)自定義按鍵

2019-10-21 18:53:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近做一個(gè)搜索用戶的功能,這里使用了UISearchBar。由于搜索的方式只有手機(jī)號(hào)碼,所以這里的鍵盤(pán)要限制為數(shù)字輸入,可以這么做:

self.searchBar.keyboardType = UIKeyboardTypeNumberPad;如果使用的不是搜索框而是textField輸入框,可以設(shè)置textField的鍵盤(pán)屬性來(lái)展示 self.textField.keyboardType = UIKeyboardTypeNumberPad;監(jiān)聽(tīng)事件如下所示即可。

ios,uikeyboardtype,自定義數(shù)字鍵盤(pán)

但是這里有個(gè)問(wèn)題,就是數(shù)字鍵盤(pán)上面沒(méi)有“搜索”按鈕,這樣子用戶在輸入完手機(jī)號(hào)碼后無(wú)法搜索。所以這個(gè)時(shí)候我們需要自己添加一個(gè)自定義的搜索按鈕,然后加到鍵盤(pán)上面。

解決思路如下所示:

1.自定義搜索button

2.監(jiān)聽(tīng)鍵盤(pán)出現(xiàn)的事件

3.遍歷搜索的Windows窗體,找到鍵盤(pán)的窗體,然后遍歷其子視圖,找到我們真正需要的鍵盤(pán)視圖

4.把我們自定義的按鈕加到上面找到的視圖里

這里要注意的一點(diǎn),隨著iOS SDK的不斷發(fā)展,keyboard的視圖名稱(chēng)也不斷在更新變化,當(dāng)你調(diào)試以下代碼無(wú)法得到期待的效果時(shí),請(qǐng)重新遍歷一次窗臺(tái),然后慢慢調(diào)試,找到真正需要的視圖名稱(chēng)。

解決代碼

1.自定義搜索按鈕

// 搜索按鈕_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.監(jiān)聽(tīng)鍵盤(pán)出現(xiàn)的事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowOnDelay:) name:UIKeyboardWillShowNotification object:nil];- (void)keyboardWillShowOnDelay:(NSNotification *)notification {[self performSelector:@selector(keyboardWillShow:) withObject:nil afterDelay:0];}

這里面監(jiān)聽(tīng)通知后的執(zhí)行函數(shù)并非立馬執(zhí)行查找窗體的函數(shù),是因?yàn)樵趇OS4后,鍵盤(pán)添加到窗體的事件放到了下一個(gè)EventLoop,所以我們采用了延遲的方法。

3. 遍歷視圖,并添加按鈕

- (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,uikeyboardtype,自定義數(shù)字鍵盤(pán)

以上所述是小編給大家介紹的iOS開(kāi)發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤(pán)自定義按鍵,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 墨脱县| 昌吉市| 兰州市| 莒南县| 固安县| 乃东县| 平邑县| 鹤峰县| 彰武县| 天长市| 襄樊市| 称多县| 罗江县| 涿鹿县| 景德镇市| 巴塘县| 陆丰市| 闽侯县| 阿拉尔市| 株洲市| 西安市| 永康市| 金华市| 隆林| 台东县| 渝北区| 汕尾市| 巴南区| 威信县| 金堂县| 松阳县| 会东县| 开阳县| 祥云县| 墨江| 龙岩市| 鹿泉市| 五大连池市| 荔波县| 黄平县| 郧西县|