Requirements Background
Recently did a search user's function, here uses the Uisearchbar. Because the search is only mobile phone number, so the keyboard is limited to digital input, you can do this:
<code class= "language-objective-c hljs avrasm has-numbering" style= "display:block; padding:0px; Background-color:transparent; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-top-left-radius:0px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-bottom-left-radius:0px; Word-wrap:normal; Background-position:initial initial; Background-repeat:initial initial; " >self<span class= "Hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " >.searchbar</span><span class= "Hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " >.keyboardType</span> = Uikeyboardtypenumberpad<span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >;</span></code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; Background-color:rgb (238, 238, 238); top:0px; Left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li></ul>
But here's the problem, there is no "search" button on the numeric keypad, this way the user can not search after entering the mobile phone number. So at this point we need to add a custom search button to the keyboard. solve the idea of customizing the search button to monitor the events that appear on the keyboard, traverse the Windows Form of the search, locate the keyboard's form, and then traverse its child view to find the keyboard view we really need to add our custom buttons to the view found above
One thing to note here is that as the iOS SDK continues to evolve, the keyboard view name is constantly changing, and when you debug the following code that doesn't get the desired effect, iterate over the window sill and then slowly debug to find the name of the view you really need. Resolving code 1. Custom Search button
<code class= "Hljs bash has-numbering" style= "display:block; padding:0px; Background-color:transparent; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-top-left-radius:0px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-bottom-left-radius:0px; Word-wrap:normal; Background-position:initial initial; Background-repeat:initial initial; "
>//Search Button _searchbutton = [UIButton buttonwithtype:uibuttontypecustom]; _searchbutton.frame = CGRectMake (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >0</span>, <span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >163</span>, <span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >106</span>, <span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "
>53</span>); [_searchButton <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >set</span>title:@<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "Search" </span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "
>for</span>State:UIControlStateNormal]; [_searchbutton <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >set</span>titlecolor:[uicolor Blackcolor] <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "
>for</span>State:UIControlStateNormal]; [_searchbutton addtarget:self Action: @selector (Searchbuttondidtouch:) <span class= "Hljs-keyword" style= "color: RGB (0, 0, 136); Box-sizing:border-box; " >for</span>ControlEvents:UIControlEventTouchUpInside]; </code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; Background-color:rgb (238, 238, 238); top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li></ul>
2. Listen for events that appear on the keyboard
<code class= "Hljs objectivec has-numbering" style= "display:block; padding:0px; Background-color:transparent; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-top-left-radius:0px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-bottom-left-radius:0px; Word-wrap:normal; Background-position:initial initial; Background-repeat:initial initial; " >[[<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-sizing:border-box;" >NSNotificationCenter</span> Defaultcenter] Addobserver:<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >self</span> selector:<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " > @selector </span> (keyboardwillshowondelay:) name:uikeyboardwillshownotification object:<span class= " Hljs-literal "style=" Color:rgb (0, 102, 102); Box-sizing:border-box; " >nil</span>]; -(<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >void</span>) Keyboardwillshowondelay: (<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >NSNotification</span> *) Notification {[<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-siz Ing:border-box; " >self</span> performselector:<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " > @selector </span> (keyboardwillshow:) withobject:<span class= "hljs-literal" style= "Color:rgb (0, 102, 102 ); Box-sizing:border-box; " >nil</span> afterdelay:<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "
>0</span>]; }</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; Background-color:rgb (238, 238, 238); top:0px; left:0px; margin:0px; padding:6px 0px 40px; Border-right-width: 1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li></ul>
The execution function after listening to the notification does not immediately perform the function of finding the form, because after iOS4, the event that the keyboard adds to the form is placed in the next eventloop, so we take a deferred approach. 3. Traverse the view and add a button
<code class= "Hljs objectivec has-numbering" style= "display:block; padding:0px; Background-color:transparent; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-top-left-radius:0px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-bottom-left-radius:0px; Word-wrap:normal; Background-position:initial initial; Background-repeat:initial initial; " >-(<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >void</span>) Keyboardwillshow: (<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >NSNotification</span> *) Notification {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-s Izing:border-box; " >UIView</span> *foundkeyboard = <span class= "hljs-literal" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "
>nil</span>; <span class= "Hljs-built_in" StylE= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >UIWindow</span> *keyboardwindow = <span class= "hljs-literal" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "
>nil</span>; <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >for</span> (<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-sizing:border-box;" >UIWindow</span> *testwindow in [[<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing: Border-box; " >UIApplication</span> sharedapplication] (Windows]) {<span class= "Hljs-keyword" style= "Color:rgb (0, 0 , 136); Box-sizing:border-box; " >if</span> (![ [Testwindow class] Isequal:[<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-sizing:border-box;"
>UIWindow</span> class]) {Keyboardwindow = Testwindow; <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >break</span>; }} <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (!keyboardwindow) <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "
>return</span>; <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >for</span> (__<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >strong</span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >UIView</span> *possiblekeyboard in [Keyboardwindow subviews]) {<span class= "Hljs-keyword" style= "Co Lor:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> ([[Possiblekeyboard description] Hasprefix:@<span class= "hljs-string" style= "Color:rgb (0, 136 , 0); Box-sizing:border-box; " > "<uiinputsetcontainerview" </SPAN>]) {<span class= "Hljs-keyword" style= "ColoR:rgb (0, 0, 136); Box-sizing:border-box; " >for</span> (__<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >strong</span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >UIView</span> *possiblekeyboard_2 in Possiblekeyboard<span class= "hljs-variable" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >.subviews</span>) {<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:bord Er-box; " >if</span> ([Possiblekeyboard_2<span class= "hljs-variable" style= "Color:rgb (102, 0, 102); Box-sizing: Border-box; " >.description</span> hasprefix:@<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; "
> "<uiinputsethostview" </SPAN>] {foundkeyboard = possiblekeyboard_2; }}}} <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (foundkeyboard) {<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:bord Er-box; " >if</span> ([[Foundkeyboard subviews] indexofobject:_searchbutton] = = <span class= "hljs-built_in" style= " Color:rgb (102, 0, 102); Box-sizing:border-box; "
>NSNotFound</span>) {[Foundkeyboard Addsubview:_searchbutton]; } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "
>else</span> {[Foundkeyboard Bringsubviewtofront:_searchbutton]; }}}</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; Background-color:rgb (238, 238, 238); top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; " ><li style= "Box-sizing:border-box; padding:0px 5px;">1</li><li style=" Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li><li style= "Box-sizing:border-box; padding:0px 5px; " >10</li><li style= "Box-sizing:border-box; padding:0px 5px; " >11</li><li style= "Box-sizing:border-box; padding:0px 5px; " >12</li><li style= "Box-sizing:border-box; padding:0px 5px; " >13</li><li style= "Box-sizing:border-box; padding:0px 5px; " >14</li><li style= "Box-sizing:border-box; Padding:0px 5px; " >15</li><li style= "Box-sizing:border-box; padding:0px 5px; " >16</li><li style= "Box-sizing:border-box; padding:0px 5px; " >17</li><li style= "Box-sizing:border-box; padding:0px 5px; " >18</li><li style= "Box-sizing:border-box; padding:0px 5px; " >19</li><li style= "Box-sizing:border-box; padding:0px 5px; " >20</li><li style= "Box-sizing:border-box; padding:0px 5px; " >21</li><li style= "Box-sizing:border-box; padding:0px 5px; " >22</li><li style= "Box-sizing:border-box; padding:0px 5px; " >23</li><li style= "Box-sizing:border-box; padding:0px 5px; " >24</li><li style= "Box-sizing:border-box; padding:0px 5px; " >25</li><li style= "Box-sizing:border-box; padding:0px 5px; " >26</li><li style= "Box-sizing:border-box; padding:0px 5px; " >27</li><li style= "Box-sizing:border-box; padding:0px 5px; " >28</li><li style= "Box-siZing:border-box; padding:0px 5px; " >29</li><li style= "Box-sizing:border-box; padding:0px 5px; " >30</li><li style= "Box-sizing:border-box; padding:0px 5px; " >31</li><li style= "Box-sizing:border-box; padding:0px 5px; " >32</li><li style= "Box-sizing:border-box; padding:0px 5px; " >33</li></ul>
In this way we have completed the need to add a custom button.
If anything is wrong, please advise.
This article solves the idea of the main reference online many predecessors of the solution ideas, thanks to their contributions.