iOS 5 ways to close and exit a keyboard

  • 2020-12-07 04:29:52
  • OfStack

iOS - 5 ways to close keyboard and exit keyboard

1. Click outside the edit area (UIView)

2. Click outside the edit area (UIControl)

3. Use the button that makes the keyboard go away

4. Use judgment input characters

5, about the keyboard masking problem

1. Click outside the edit area (UIView)

This is one kind of very intuitive approach, when no longer need to use the virtual keyboard, just click on the virtual keyboard and editing area, you can pack up the keyboard, the following code is the built-in touch events in UIView method function, you can refer to Touch Panel/touch screen/basic way of the pressure sensor 1, find out more about the method of touch events function.


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
  if (![myTextView isExclusiveTouch]) { 
    [myTextView resignFirstResponder]; 
  } 
}

If you want to use this method, please remember that you must use Custom Class 1 to operate the screen.
Custom Class for the screen is UIView

2. Click outside the edit area (UIControl)

The way to fold the virtual keyboard is the same, but if your touch event is already full of code, consider using the Touch Up Inside event to fold the keyboard by linking the following code to UIControl Up Inside event.


- (IBAction)dismissKeyboard:(id)sender { 
  [myTextView resignFirstResponder]; 
}

If you want to use this method, remember that the Custom Class 1 you are working with must be UIControl.

Custom Class of the screen is UIControl

Associate the method of folding the keyboard with the UIControl event

3. Use the button that makes the keypad

When there is no place outside the editing area to click to put away the keyboard, it is also a good method to make a button to put away the current virtual keyboard. Since the button must appear on the virtual keyboard in order to be displayed on the screen, NSNotificationCenter must be used to help us judge the current status of the keyboard.
First, in the viewDidLoad: event, register to NSNotificationCenter, telling NSNotificationCenter our doneButtonshow: method function.


- (void)viewDidLoad { 
  [super viewDidLoad]; 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil]; 
}

Now every time the virtual keyboard appears, our custom doneButtonshow: method function is automatically called, and we just define the method within that method function where the button appears.


-(void) doneButtonshow: (NSNotification *)notification { 
  doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
  doneButton.frame = CGRectMake(0, 228, 70, 35); 
  [doneButton setTitle:@" The editing " forState: UIControlStateNormal]; 
  [doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside]; 
  
  [self.view addSubview:doneButton]; 
}

Finally, implement the hideKeyboard: method function when the button is pressed. Be sure to remove the button from the function.


-(void) hideKeyboard { 
  [doneButton removeFromSuperview]; 
  [myTextView resignFirstResponder]; 
} 

4. Use judgment to enter characters

If you want to use a particular character (such as return newline characters) to fold the keyboard, you must first use the @interface section in the category. You can refer to how to use the Protocol protocol 1 for more information about the protocol.

After the agreement, then make the agreement within textView: shouldChangeTextInRange: replacementText: function method, this method function to trigger the character input, and comes back BOOL value represents the characters of whether to, the following code is to function in this method, using the judgment to pack up the virtual keyboard input characters (judging character return newline characters).


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 
  if ([text isEqualToString:@"\n"]) { 
    [myTextView resignFirstResponder]; 
    return NO; 
  } 
  return YES; 
}

Finally, don't forget to point the UITextView proxy object to yourself in the viewDidLoad: event so that the program can correctly find the class of the implementation protocol method function.


- (void)viewDidLoad { 
  [super viewDidLoad]; 
  myTextView.delegate = self; 
}

5. The question of keyboard masking

If you have a problem with the keyboard masking editing area in your implementation, you can refer to article 1 of Animation to block the UITextField keypad. The Animation function of Core Graphic can move the editing area while the keyboard appears to solve the masking problem.

Above is the IOS close keyboard exit keyboard 5 ways of data collation, thank you for your support to this site!


Related articles: