Summary of UIKeyboard keyboard View usage in iOS

  • 2020-06-07 05:21:58
  • OfStack

1. Keyboard style
The UIKit framework supports eight keyboard styles.

typedef enum { 
    UIKeyboardTypeDefault,                // Default keyboard: Supports all characters  
    UIKeyboardTypeASCIICapable,           // support ASCII Default keyboard  
    UIKeyboardTypeNumbersAndPunctuation,  // Standard phone keyboard, support +*# Such symbols  
    UIKeyboardTypeURL,                    // URL The keyboard, .com Button; Only support URL character  
    UIKeyboardTypeNumberPad,              // The keypad  
    UIKeyboardTypePhonePad,               // The phone keyboard  
    UIKeyboardTypeNamePhonePad,           // The phone keyboard also supports the input of a person's name  
    UIKeyboardTypeEmailAddress,           // A keyboard for entering E-mail addresses  
} UIKeyboardType; 

Usage cases:

textView.keyboardtype = UIKeyboardTypeNumberPad;

2. Keyboard appearance

typedef enum { 
    UIKeyboardAppearanceDefault,    // Default appearance: light gray  
    UIKeyboardAppearanceAlert,      // Dark grey / Graphite color  
} UIKeyboardAppearance;

Usage cases:


3. The enter key

typedef enum { 
    UIReturnKeyDefault,  // Default: Grey button, marked Return
    UIReturnKeyGo,  // marked Go Blue button
    UIReturnKeyGoogle,  // marked Google Blue button for search
    UIReturnKeyJoin,  // marked Join Blue button
    UIReturnKeyNext,  // marked Next Blue button
    UIReturnKeyRoute,  // marked Route Blue button
    UIReturnKeySearch,  // marked Search Blue button
    UIReturnKeySend,  // marked Send Blue button
    UIReturnKeyYahoo,  // marked Yahoo! Blue button for search
    UIReturnKeyDone,  // marked Done Blue button
    UIReturnKeyEmergencyCall,  // Emergency call button
} UIReturnKeyType; 

Usage cases:


4. Automatic capitalization

typedef enum { 
    UITextAutocapitalizationTypeNone, // Not automatically capitalized  
    UITextAutocapitalizationTypeWords, // Capitalize words  
    UITextAutocapitalizationTypeSentences, // The sentence begins with a capital letter  
    UITextAutocapitalizationTypeAllCharacters, // All caps  
} UITextAutocapitalizationType; 

Usage cases:

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;


typedef enum { 
    UITextAutocorrectionTypeDefault,// The default  
    UITextAutocorrectionTypeNo,// Non-Autocorrect  
    UITextAutocorrectionTypeYes,// Automatic correction  
} UITextAutocorrectionType;

Usage cases:

textField.autocorrectionType = UITextAutocorrectionTypeYes;

6. Secure text input


Turn on secure input for passwords or private data. Autocorrect and cache are disabled.
7. Turn on the keyboard to cover the View
By default, opening the keyboard will obscure view below, causing 1 bit of trouble, but it's not a big problem, we can fix it with a little help.
The first thing to realize is that the keyboard height is fixed, but since IOS 5.0, it doesn't seem to be 216 keyboard height anymore, but never mind, let's just adjust it:
Our approach is to move self.view up 216px as soon as textField(possibly other controls) receives a pop-up keyboard event (let's take iPhone portrait for example).
First we need to set the textField agent, which is the current controller.


Then we implement the following three delegate methods in the current controller:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ // When the touch point textField Internally, this method is called when you start editing. textField Will be first responder  
       NSTimeInterval animationDuration = 0.30f;     
      CGRect frame = self.view.frame; 
      frame.origin.y -=216; 
      frame.size.height +=216; 
      self.view.frame = frame; 
       [UIView beginAnimations:@"ResizeView" context:nil]; 
       [UIView setAnimationDuration:animationDuration]; 
       self.view.frame = frame;                 
       [UIView commitAnimations];                 

- (BOOL)textFieldShouldReturn:(UITextField *)textField  
{// When the user presses ruturn Focus from textField Remove it and the keyboard will disappear  
        NSTimeInterval animationDuration = 0.30f; 
        CGRect frame = self.view.frame;     
        frame.origin.y +=216;       
        frame.size. height -=216;    
        self.view.frame = frame; 
    //self.view Move back to the original position    
    [UIView beginAnimations:@"ResizeView" context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
        self.view.frame = frame;                 
        [UIView commitAnimations]; 
        [textField resignFirstResponder];    

Related articles: