比來做一個搜刮用戶的功效,這裡應用了UISearchBar。因為搜刮的方法只要手機號碼,所以這裡的鍵盤要限制為數字輸出,可以這麼做:
self.searchBar.keyboardType = UIKeyboardTypeNumberPad;假如應用的不是搜刮框而是textField輸出框,可以設置textField的鍵盤屬性來展現 self.textField.keyboardType = UIKeyboardTypeNumberPad;監聽事宜以下所示便可。
然則這裡有個成績,就是數字鍵盤下面沒有“搜刮”按鈕,如許子用戶在輸出完手機號碼後沒法搜刮。所以這個時刻我們須要本身添加一個自界說的搜刮按鈕,然後加到鍵盤下面。
處理思緒以下所示:
1.自界說搜刮button
2.監聽鍵盤湧現的事宜
3.遍歷搜刮的Windows窗體,找到鍵盤的窗體,然後遍歷其子視圖,找到我們真正須要的鍵盤視圖
4.把我們自界說的按鈕加到下面找到的視圖裡
這裡要留意的一點,跟著IOS SDK的赓續成長,keyboard的視圖稱號也赓續在更新變更,當你調試以下代碼沒法獲得等待的後果時,請從新遍歷一次窗台,然後漸漸調試,找到真正須要的視圖稱號。
處理代碼
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.監聽鍵盤湧現的事宜
[[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. 遍歷視圖,並添加按鈕
- (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數字鍵盤自界說按鍵】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!