Several Methods to disable ios virtual keyboard and ios virtual keyboard

Source: Internet
Author: User

Several Methods to disable ios virtual keyboard and ios virtual keyboard

In iOS application development, three types of view objects enable the virtual keyboard for input operations, but there is no automated method for disabling the virtual keyboard. We need to implement this by ourselves. The three view objects are UITextField, UITextView, and UISearchBar. Here we will introduce several methods to disable the virtual keyboard in UITextField.

 

(Miki westward journey @ mikixiyou original link: http://mikixiyou.iteye.com/blog/1753330)

The first method is to use its delegate method textFieldShouldReturn in UITextFieldDelegate: to close the virtual keyboard. Implement this method in the class where the UITextField view object, such as birdNameInput, is located.

 

C code
  • -(BOOL) textFieldShouldReturn :( UITextField *) textField {
  • If (textField = self. birdNameInput) | (textField = self. locationInput )){
  • [TextField resignFirstResponder];
  • }
  • Return YES;
  • }
  • - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if ((textField == self.birdNameInput) || (textField == self.locationInput)) {
            [textField resignFirstResponder];
        }
        return YES;
    }

     

    In this way, after the virtual keyboard is opened in the input box birdNameInput, the return key of the keyboard will be automatically disabled.
    The second method is to change the Return Key in the birdNameInput attribute to done, and then define a method to connect to the Did End On Exit of the Done Key. Press the done key to trigger this event to close the virtual keyboard. The method is as follows:

     

    C code
  • -(IBAction) textFieldDoneEditing :( id) sender
  • {
  • [Sender resignFirstResponder];
  • }
  • - (IBAction) textFieldDoneEditing:(id)sender
    {
            [sender resignFirstResponder];
    }

     

     

    Both methods are used to close the virtual keyboard by tapping a key on it. This is a precise operation, and the finger is not as easy as the mouse. Therefore, both methods are not the best at the UI Layer. On an iphone or ipad screen, the virtual keyboard occupies a limited area. Close the virtual keyboard by tapping outside the area of the virtual keyboard.

     

    Method 3: Close the virtual keyboard by tapping the blank area outside the keyboard. Define a UITapGestureRecognizer object in the viewDidLoad method of the View Controller class to which birdNameInput belongs, and assign it to its view.

    C code
  • UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (dismissKeyboard)];
  • [Self. view addGestureRecognizer: tap];
  • [Tap release];
  • UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
    [tap release];

     

    Define the dismissKeyboard method called by the selector.

    C code
  • -(Void) dismissKeyboard {
  • [BirdNameInput resignFirstResponder];
  • }
  • -(void)dismissKeyboard {
           [birdNameInput resignFirstResponder];
    }

    If there are multiple textfields on the screen, listing them one by one is troublesome. Modify the method as follows:

    C code
  • -(Void) dismissKeyboard {
  • NSArray * subviews = [self. view subviews];
  • For (id objInput in subviews ){
  • If ([objInput isKindOfClass: [UITextField class]) {
  • UITextField * theTextField = objInput;
  • If ([objInput isFirstResponder]) {
  • [TheTextField resignFirstResponder];
  • }
  • }
  • }
  • }
  • -(void)dismissKeyboard {
        NSArray *subviews = [self.view subviews];
        for (id objInput in subviews) {
            if ([objInput isKindOfClass:[UITextField class]]) {
                UITextField *theTextField = objInput;
                if ([objInput isFirstResponder]) {
                    [theTextField resignFirstResponder];
                }
            }
        }
    }

     

    If the view object on the screen is complex, let alone. This method is used to encode the creation of a new gesture object. You can also directly use the interface builder graphical development tool to pull a gesture object to the View Controller class in the storyboard, and then create an IBACTION for this gesture object. The name can be dismissKeyboard.
    Method 4: Click the blank area outside the keyboard to close the virtual keyboard. Drag the view on the screen, that is, the parent view of textField, to a touch down event and connect to a method that can disable the virtual keyboard. If the view does not have a touch down event, you can change the view's parent class from UIView to UIButton. First define and implement a method backgroundTap :.

     

    C code
  • -(IBAction) backgroundTap :( id) sender
  • {
  • NSArray * subviews = [self. view subviews];
  • For (id objInput in subviews ){
  • If ([objInput isKindOfClass: [UITextField class]) {
  • UITextField * theTextField = objInput;
  • If ([objInput isFirstResponder]) {
  • [TheTextField resignFirstResponder];
  • }
  • }
  • }
  • }
  • - (IBAction) backgroundTap:(id)sender
    {
            NSArray *subviews = [self.view subviews];
        for (id objInput in subviews) {
            if ([objInput isKindOfClass:[UITextField class]]) {
                UITextField *theTextField = objInput;
                if ([objInput isFirstResponder]) {
                    [theTextField resignFirstResponder];
                }
            }
        }
    }

     

    Select the Touch Down event in the background view and connect to backgroundTap. In this way, you only need to tap the area outside the virtual keyboard to close the virtual keyboard. These methods use the resignFirstResponder method to disable the virtual keyboard. There are other methods.

     

    Method 5: Use endEditing: The method overwrites this method in the View Controller class where it is located.

     

    C code
  • -(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
  • [[Self view] endEditing: YES];
  • }
  • - (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 that is currently the first responder. if it finds one, it asks that text field to resign as first responder. if the force parameter is set to YES, the text field is never even asked; it is forced to resign. however, if the screen is complex, there are many buttons in areas outside the virtual keyboard. When you tap these areas, You may click these buttons so that the virtual keyboard cannot be closed. If it is not easy to find a blank area without buttons and there are hidden view objects, it is difficult to close the virtual keyboard by tapping the area outside the virtual keyboard.

     

    Method 6: override hitTest: withEvent: Disable the virtual keyboard

     

    On stackoverflow.com, someone summarized this. Using hitTest: withEvent is the best and easiest solution.

     

    I think the easiest (and best) way to do this is to subclass your global view and use hitTest: withEvent method to listen to any touch. touches on keyboard aren't registered, so hitTest: withEvent is only called when you touch/scroll/swipe/pinch... somewhere else, then call [self endEditing: YES]. this is better than using touchesBegan because touchesBegan are 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 screen because in a complexe and dynamic user interface, you can't put dim screen every where. moreover, it doesn't block other actions, you don't need to tap twice to select a button outside (like in the case of a UIPopover ). also, it's better than calling [textField resignFirstResponder], because you may have plain text fields on screen, so this works for all of them.

     

    Therefore, I create a View class that inherits UIView. In this view class, override the hitTest: withEvent: method and add the [self endEditing: YES] method.

    C code
  • -(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {
  • UIView * result = [super hitTest: point withEvent: event];
  • [Self endEditing: YES]
  • Return result;
  • }
  • - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *result = [super hitTest:point withEvent:event];
    [self endEditing:YES]
    return result;
    }

     

    I changed the class of the main view of the View Controller to this new view class. In this way, the virtual keyboard is closed when you tap anywhere on the screen. This method is the easiest and best way to disable the virtual keyboard. HitTest: withEvent: This method can also implement many complex functions. The implementation of hitTest: withEvent: in UIResponder does the following:

    • It cballs pointInside: withEvent: of self
    • If the return is NO, hitTest: 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: returns 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, you might override hitTest: withEvent to do something differently. in your cases, overriding pointInside: withEvent: is simpler and still provides enough options to tweak event handling in your application.


    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.