Custom buttons for iOS keyboards and ios keyboards
UIKeyboardTypeNumberPad: Custom keyboard buttons
Recently, a user search function is provided. UISearchBar is used here. Because the search method only involves mobile phone numbers, the keyboard here must be limited to numbers. You can do this:
Self. searchBar. keyboardType = UIKeyboardTypeNumberPad;
If you are not using the search box but the textField input box, you can set the keyboard attribute of textField to display
Self. textField. keyboardType = UIKeyboardTypeNumberPad;
The listener event is as follows.
However, there is a problem, that is, there is no "Search" button on the keyboard, so that the user cannot search after entering the mobile phone number. Therefore, we need to add a Custom Search button and add it to the keyboard.
Solution
Note that with the development of iOS SDK, the view name of the keyboard is constantly updated and changed. When you debug the following code, repeat the window sill and debug it slowly to find the view name you really need.
Solution Code 1. Custom Search button
// Search button _ searchButton = [UIButton buttonWithType: UIButtonTypeCustom]; _ searchButton. frame = CGRectMake (0,163,106, 53); [_ searchButton setTitle: @ "Search" forState: UIControlStateNormal]; [_ searchButton setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [_ searchButton addTarget: self action: @ selector (SearchButtonDidTouch :) forControlEvents: UIControlEventTouchUpInside];
2. Listen to Keyboard Events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowOnDelay:) name:UIKeyboardWillShowNotification object:nil];- (void)keyboardWillShowOnDelay:(NSNotification *)notification { [self performSelector:@selector(keyboardWillShow:) withObject:nil afterDelay:0];}
The execution function after listening for the notification is not immediately executed to find the form function, because after iOS4, the events added to the form on the keyboard are placed in the next EventLoop, so we useLatency.
3. traverse the view and add a button
- (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]; } }}