天天看點

iOS 鍵盤高度及擷取鍵盤高度的方法

一、擷取鍵盤高度的方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //增加監聽,當鍵盤出現或改變時接收消息
    
    //加一個textField,以便可以彈出鍵盤
    UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];
    textField.backgroundColor = [UIColor redColor];
    textField.keyboardType = UIKeyboardTypeNumberPad;  //鍵盤類型
    [self.view addSubview:textField];
    
    //增加監聽,當鍵盤出現或改變時收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加監聽,當鍵退出時收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}


//當鍵盤出現或改變時調用
- (void)keyboardWillShow:(NSNotification *)aNotification {
    //擷取鍵盤的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;   //height 就是鍵盤的高度
    int width = keyboardRect.size.width;     //width  鍵盤寬度
}

//當鍵退出時調用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    
}
           

//點選textField 就會走- (void)keyboardWillShow:(NSNotification *)aNotification()這個方法,即可擷取鍵盤的高度和寬度。

二、各種類型鍵盤的寬度和高度

UIKit架構支援8種風格鍵盤。
typedef enum {  
    UIKeyboardTypeDefault,                // 預設鍵盤:支援所有字元   
    UIKeyboardTypeASCIICapable,           // 支援ASCII的預設鍵盤   
    UIKeyboardTypeNumbersAndPunctuation,  // 标準電話鍵盤,支援+*#等符号   
    UIKeyboardTypeURL,                    // URL鍵盤,有.com按鈕;隻支援URL字元   
    UIKeyboardTypeNumberPad,              //數字鍵盤   
    UIKeyboardTypePhonePad,               // 電話鍵盤   
    UIKeyboardTypeNamePhonePad,           // 電話鍵盤,也支援輸入人名字   
    UIKeyboardTypeEmailAddress,           // 用于輸入電子郵件位址的鍵盤   
} UIKeyboardType; 

 

用法用例:
textField.keyboardtype= UIKeyboardTypeNumberPad;
           
iPhoneX/Xs iPhoneXR/Max  iPhone 6plus iPhone 6  iPhone 5   ipad Air    ipad2
中文   375/335      414/346       414/271    375/258   320/216    768/264    768/264              

英文   375/291      414/301       414/226    375/216   320/216    768/264    768/264
           
除了X系列,以下幾種鍵盤類型幾乎一樣,鍵盤高度也是一樣的

UIKeyboardTypeAlphabet 
UIKeyboardTypeASCIICapable 
UIKeyboardTypeDefault
UIKeyboardTypeEmailAddress
UIKeyboardTypeNamePhonePad
UIKeyboardTypeNumbersAndPunctuation(數字和标點符号)
UIKeyboardTypeTwitter
UIKeyboardTypeURL
UIKeyboardTypeWebSearch

5.5吋271
4.7吋258
4.0吋253

②以下幾種鍵盤為數字類型的鍵盤,鍵盤高度也是一樣的

UIKeyboardTypeDecimalPad(帶小數點的數字鍵盤)
UIKeyboardTypeNumberPad(純數字鍵盤)
UIKeyboardTypePhonePad(帶*+#,;的數字鍵盤)

5.5吋226
4.7吋216
4.0吋216
           

參考:https://blog.csdn.net/ochenmengo/article/details/85840552