Uikeyboardtypenumberpad Digital Keypad custom button _ios for IOS development

Source: Internet
Author: User

Recently did a search user's function, here uses the Uisearchbar. Because the search is only mobile phone number, so the keyboard here to limit the number of input, you can do this:

Self.searchBar.keyboardType = Uikeyboardtypenumberpad; If you are using a search box instead of a textfield input box, you can set TextField keyboard properties to show Self.textField.keyboardType = Uikeyboardtypenumberpad; The Listener event looks like this.

But the problem here is that there is no "search" button on the numeric keypad, which means users can't search after they have entered their phone number. So this time we need to add a custom search button to the keyboard.

The solution is as follows:

1. Custom Search button

2. Monitor the events that appear on the keyboard

3. Traverse the search Windows Form, locate the keyboard form, and then traverse its child view to find the keyboard view we really need

4. Add our customized buttons to the view found above

To note here, as the iOS SDK continues to evolve, keyboard's view name is also constantly changing, when you debug the following code can not get the desired effect, please iterate over the windowsill, and then slowly debug, find the real need for the view name.

Resolve Code

1. Custom Search button

Search button
_searchbutton = [UIButton buttonwithtype:uibuttontypecustom];
_searchbutton.frame = CGRectMake (0, 163,);
[_searchbutton settitle:@ "search" forstate:uicontrolstatenormal];
[_searchbutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

2. Monitor the events that appear on the keyboard

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillshowondelay:) Name: Uikeyboardwillshownotification Object:nil];
-(void) Keyboardwillshowondelay: (nsnotification *) notification {
[self performselector: @selector ( Keyboardwillshow:) Withobject:nil afterdelay:0];

The executive function that listens for notifications does not immediately perform the lookup form function because after the iOS4, the keyboard added to the form is placed next to the EventLoop, so we use the delay method.

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

The above is a small set to introduce the development of iOS Uikeyboardtypenumberpad Digital Keyboard customization button, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.