iOS獲取鍵盤彈出高度,監聽鍵盤彈出退出

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

    [[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;


    [self.view bringSubviewToFront:self.chatSessionInputBarControl];

    

        [UIView animateWithDuration:0.4 animations:^{

            self.midView.frame = CGRectMake(0, 64, VIEW_WIDTH, 42);

            self.scrollView.frame = CGRectMake(0, 64+42, self.view.frame.size.width, VIEW_HEIGHT-height-64-42-50);

            self.conversationMessageCollectionView.contentOffset = CGPointMake(0, 820);

        }];

}


//當鍵退出時調用

- (void)keyboardWillHide:(NSNotification *)aNotification {

    [UIView animateWithDuration:0.4 animations:^{

        self.midView.frame = CGRectMake(0, 160, VIEW_WIDTH, 42);

        self.scrollView.frame = CGRectMake(0, 160+42, self.view.frame.size.width, self.view.frame.size.height-160-50-42);

        self.conversationMessageCollectionView.contentOffset = CGPointMake(0, 660);

    }];

}



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