Custom buttons for iOS keyboards and ios keyboards

Source: Internet
Author: User

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];        }    }}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.