動態獲取鍵盤的高度

- (void)registerForKeyboardNotifications{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter]  addObserver:self
                                              selector:@selector(keyboardWasHidden:)
                                                  name:UIKeyboardWillHideNotification
                                                object:nil];
    
}
- (void)keyboardWasShown:(NSNotification *)notif{
    
    CGRect keyboardEndFrameWindow;
    [[notif.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardEndFrameWindow];
    
    double keyboardTransitionDuration;
    [[notif.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&keyboardTransitionDuration];
    
    UIViewAnimationCurve keyboardTransitionAnimationCurve;
    [[notif.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&keyboardTransitionAnimationCurve];
    
    CGRect keyboard = [self.view convertRect:keyboardEndFrameWindow fromView:nil];
    
    CGRect rect=CGRectMake(0, ScreenHeight-keyboard.size.height-40, ScreenWidth, 40);
    [UIView animateWithDuration:0.2 animations:^{
        _sendMsgView.frame=rect;
    }];
}

- (void) keyboardWasHidden:(NSNotification *) notif{
    CGRect rect=CGRectMake(0, ScreenHeight-90, ScreenWidth, 40);
    [UIView animateWithDuration:0.2 animations:^{
        _sendMsgView.frame=rect;
    }];
}
發佈了71 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章