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

你可能感兴趣的:(iOS,Object-c)