標籤:ios 擷取 enter tor idt undefined obj mat 連結
-
#pragma mark - 鍵盤通知- (void)addNoticeForKeyboard { //註冊鍵盤出現的通知 [[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 *)notification { //擷取鍵盤高度,在不同裝置上,以及中英文下是不同的 CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; //計算出鍵盤頂端到inputTextView panel底端的距離(加上自訂的緩衝距離INTERVAL_KEYBOARD) CGFloat offset = (textView.frame.origin.y+textView.frame.size.height+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight); // 取得鍵盤的動畫時間,這樣可以在視圖上移的時候更連貫 double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //將視圖上移計算好的位移 if(offset > 0) { [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height); }]; }}///鍵盤消失事件- (void) keyboardWillHide:(NSNotification *)notify { // 鍵盤動畫時間 double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //視圖下沉恢複原狀 [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); }];}
奮鬥的蝸牛
連結:http://www.jianshu.com/p/4e235e952b0c
來源:簡書
iOS-當輸入框被鍵盤遮擋時讓整個view上移