監聽鍵盤高度

在遇到有輸入的情況下。由於現在鍵盤的高度是動態變化的。中文輸入與英文輸入時高度不同。所以輸入框的位置也要做出相應的變化

添加監聽事件:

//增加監聽,當鍵盤出現或改變時收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification//這個是帶系統動畫的

//UIKeyboardDidShowNotification 鍵盤出來後再出現,不帶動畫

                                              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

{

    

}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章