iOS键盘高度的获取

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //增加监听,当键盘出现或改变时收出消息
    [[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;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{

}

高度值其实就只有两种类型,一个是Default一个是Number

①以下几种键盘类型几乎一样,键盘高度也是一样的

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

 

 

posted on 2016-12-06 17:34  ming1025  阅读(1618)  评论(0编辑  收藏  举报