Several ways to turn off the iOS virtual keyboard

Source: Internet
Author: User

In iOS app development, there are three types of view objects that open the virtual keyboard for input, but how to turn off the virtual keyboard without providing an automated method. This needs our own realization. These three types of view objects are Uitextfield,uitextview and Uisearchbar, respectively. Here are a few ways to turn off the virtual keyboard in Uitextfield.

(Miki West Tour @mikixiyou original link: http://mikixiyou.iteye.com/blog/1753330)

The first method, using the method Textfieldshouldreturn in its delegate uitextfielddelegate: to close the virtual keyboard. Implement this method in the class where the Uitextfield view object is located, such as Birdnameinput.

C code
    1. -(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {
    2. if ((TextField = = Self.birdnameinput) | | (TextField = = self.locationinput)) {  
    3. [TextField Resignfirstresponder];
    4. }
    5. return YES;
    6. }
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {    if (TextField = = Self.birdnameinput) | | (TextField = = self.locationinput)) {        [TextField resignfirstresponder];    }    return YES;}

This way, after you open the virtual keyboard in the input box birdnameinput, tapping the return key of the keyboard will automatically turn off the virtual keyboard.
The second method modifies the Birdnameinput property of return key to done, and then defines a method and the done key for the Do End on exit connection. This event is triggered by tapping the done key to turn off the virtual keyboard. The methods are defined as follows:

C code
    1. -(Ibaction) textfielddoneediting: (ID) sender
    2. {
    3. [Sender Resignfirstresponder];
    4. }
-(Ibaction) textfielddoneediting: (ID) sender{        [sender Resignfirstresponder];}

Both of these methods are to tap a key on the virtual keyboard to close it. This is a precise operation, and the finger is not like a mouse, and doing this is not easy. As a result, neither of these approaches is the best approach for the UI level. On the iphone or ipad screen, the virtual keyboard occupies a limited amount of space. Turn off the virtual keyboard by tapping an area other than the virtual keyboard.

The third method is to turn off the virtual keyboard by tapping an empty area outside the keyboard. Define a UITapGestureRecognizer object in the Viewdidload method of the View controller class that birdnameinput belongs to, and then assign it to its view.

C code
    1. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Dismisskeyboard)] ;
    2. [Self.view Addgesturerecognizer:tap];
    3. [Tap release];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initwithtarget:self action: @selector ( Dismisskeyboard)]; [Self.view Addgesturerecognizer:tap]; [Tap release];

Then define the method Dismisskeyboard called by the selector.

C code
    1. -(void) Dismisskeyboard {
    2. [Birdnameinput Resignfirstresponder];
    3. }
-(void) Dismisskeyboard {       [birdnameinput resignfirstresponder];}

If there's more than one textfield on the screen, it's a bit of a hassle to list them one by one. Then modify the method as follows:

C code
  1. -(void) Dismisskeyboard {
  2. Nsarray *subviews = [Self.view subviews];
  3. For (id objinput in subviews) {
  4. if ([Objinput Iskindofclass:[uitextfield class]]) {
  5. Uitextfield *thetextfield = objinput;
  6. if ([Objinput Isfirstresponder]) {
  7. [Thetextfield Resignfirstresponder];
  8. }
  9. }
  10. }
  11. }
-(void) Dismisskeyboard {    Nsarray *subviews = [Self.view subviews];    For (ID objinput in subviews) {        if ([Objinput Iskindofclass:[uitextfield class]]) {            Uitextfield *thetextfield = ob Jinput;            if ([Objinput Isfirstresponder]) {                [Thetextfield resignfirstresponder];}}}    

If the View object on this screen is complex, it is a matter of other things. This method is coded to create a new gesture object. You can also directly use the Interface Builder Graphical development tool, pull a gesture object into the View controller class in storyboard, and then set up a ibaction for this gesture object, which can be dismisskeyboard.
The fourth method is to turn off the virtual keyboard by tapping an empty area outside the keyboard. Drag a touch down event out of the screen view, the parent view of TextField, and connect to a method that turns off the virtual keyboard. If the view does not have a touch-down event, you can change the parent class of the view from UIView to UIButton. First define and implement a method Backgroundtap:.

C code
  1. -(Ibaction) Backgroundtap: (ID) sender
  2. {
  3. Nsarray *subviews = [Self.view subviews];
  4. For (id objinput in subviews) {
  5. if ([Objinput Iskindofclass:[uitextfield class]]) {
  6. Uitextfield *thetextfield = objinput;
  7. if ([Objinput Isfirstresponder]) {
  8. [Thetextfield Resignfirstresponder];
  9. }
  10. }
  11. }
  12. }
-(Ibaction) Backgroundtap: (ID) sender{        nsarray *subviews = [Self.view subviews];    For (ID objinput in subviews) {        if ([Objinput Iskindofclass:[uitextfield class]]) {            Uitextfield *thetextfield = ob Jinput;            if ([Objinput Isfirstresponder]) {                [Thetextfield resignfirstresponder];}}}    

Then select the touch down event for the background view and connect Backgroundtap: you can. This allows you to turn off the virtual keyboard by tapping the area outside the virtual keyboard. These methods use the Resignfirstresponder method to turn off the virtual keyboard, and there are other methods.

The fifth method, using the Endediting: method, overrides this method in the view controller class in which it is located.

C code
    1. -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {
    2. [[Self view] endediting:yes];
    3. }
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {      [[self view] endediting:yes];}

This method looks at the current view and its Subview hierarchy for the text field, which is currently the first responder. If it finds one, it asks that text field to resign as first responder. If The force parameter are set to YES, the text field is never even asked; It's forced to resign. However, if this screen is complex, there are many buttons in the area outside the virtual keyboard. Tapping these areas may tap these buttons so that the virtual keyboard cannot be turned off. If finding a blank area without a button is not easy and there are hidden view objects, it is difficult to turn off the virtual keyboard by tapping an area other than the virtual keyboard.

Sixth method, overriding Hittest:withevent: method to close the virtual keyboard

On the stackoverflow.com, someone summed it up. Say using Hittest:withevent: The method is the best and easiest solution.

I think the easiest (and best) the same-to-do-is-subclass your global view and use-hittest:withevent method to listen T o any touch. Touches on keyboard aren ' t registered, so hittest:withevent are only called when you touch/scroll/swipe/pinch ... somewhere else, then call [self endediting:yes]. This is better than using Touchesbegan because Touchesbegan be not called if you click on a button on top of the view. It is better than uitapgesturerecognizer which can ' t recognize a scrolling gesture for example. It is also better than using a dim screens because in a complexe and the dynamic user interface, you can ' t put the dim screen every where. Moreover, it doesn ' t block other actions, you don ' t need to tap twice to select a button outside (as in the case of a UI Popover). Also, it's better than calling [TextField Resignfirstresponder], because you could have many the text fields in screen, so this Works for all of them.

So I'm going to build a view class that inherits UIView. In this view class, override the Hittest:withevent: method to add the [self Endediting:yes] method.

C code
    1. -(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) Event {
    2. UIView *result = [Super Hittest:point withevent:event];
    3. [Self Endediting:yes]
    4. return result;
    5. }
-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) Event {UIView *result = [Super Hittest:point withevent:event];[ Self endediting:yes]return Result;}

I have modified the view controller's main viewing owning class to this new view class. This will turn off the virtual keyboard if you tap anywhere on the screen. This method is the simplest and best way to turn off the virtual keyboard. Use good hittest:withevent: This method can also achieve a lot of very complex functions. The implementation of HitTest:withEvent:in Uiresponder does the following:

    •     It calls pointInside:withEvent:of self
    •     If The return was NO, hit Test:withEvent:returns Nil. The end of the story.
    •     If The return is YES, it sends HitTest:withEvent:messages to its subviews. It starts from the top- Level Subview, and continues to other views until a Subview returns a Non-nil object, or all subviews receive the message.
    •     If A Subview returns a Non-nil object in the first time, the first HitTest:withEvent:return s that object. The end of the story.
    •     If No Subview Returns a Non-nil object, the first hitTest:withEvent:returns self

This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually. However, might override hittest:withevent to do something differently. In many cases, overriding pointInside:withEvent:is simpler and still provides enough options to tweak event handling in Y Our application.

Several ways to turn off the iOS virtual keyboard

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.