2017-03-17 84 views
-1

我有一個UIView其中加入作爲子視圖UIScrollViewUIView包含UITextFieldUITextView爲了避免鍵盤隱藏我已經註冊爲keyboardWasShownkeyboardWillHide通知的字段和我有這片編寫的代碼滾動的UITextField當鍵盤顯示出來

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    isKeyboardUp = YES; 

    // Step 1: Get the size of the keyboard. 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    CGRect TextFieldFrame; 
    if(activeTextField) 
    { 
     TextFieldFrame = [activeTextField.superview convertRect:activeTextField.frame toView:self.view]; 
    } 
    else 
    { 
     TextFieldFrame = [activeTextView.superview convertRect:activeTextView.frame toView:self.view]; 
    } 

    [self.scrollView scrollRectToVisible:TextFieldFrame animated:YES]; 
} 

- (void) keyboardWillHide:(NSNotification *)notification 
{ 
    isKeyboardUp = NO; 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
} 

這裏activeTextFieldactiveTextView是目前使用文本字段這部作品很好在大多數情況下,但在少數失敗可以有人建議,如果有什麼不對的實施。

+1

使用本https://github.com/hackiftekhar/IQKeyboardManager – karthikeyan

+0

我不打算使用任何第三方框架。 –

+0

您是否將委託設置爲所有UITextFields? –

回答

0

是的,如果你使用的是滾動型,那麼你可以使用simplty TPKEYBOARDTYPING 它可用於可可豆莢或者你可以從GitHub它自身得到它。 添加其文件,然後只需轉到故事板,然後在scrollview中將其類更改爲tpkeyboardtypingscrollview。

它會給所有必要的東西,如返回鍵盤退出,並將改變scrollView自身的偏移量,使您的textFields不會隱藏在鍵盤後面。

0

試試這個

-(void)keyboardWillShow:(NSNotification*)notification { 

NSDictionary *info = [notification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
keyboardHeight = kbSize.height; 
[self updateScrollViewPosition]; 
} 

-(void)keyboardDidChange:(NSNotification *)notification { 

NSDictionary *info = [notification userInfo]; 
CGSize kbSizeBegin = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
CGSize kbSizeEnd = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
if (kbSizeBegin.height != kbSizeEnd.height) { 
    keyboardHeight = kbSizeEnd.height; 
    if (activeTextField && [activeTextField isFirstResponder]) { 
     [self updateScrollViewPosition]; 
    } 
} 
} 

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

keyboardHeight = 0; 
activeTextField = nil; 
[self resignAllTextFields]; 
} 

#pragma mark - UITextFieldDelegate Methods 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
activeTextField = textField; 
return YES; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

activeTextField = textField; 
[self updateScrollViewPosition]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 

keyboardHeight = 0; 
activeTextField = nil; 
[textField resignFirstResponder]; 
return YES; 
} 

#pragma mark - Update Method 

-(void)updateScrollViewPosition { 

if (keyboardHeight > 0 && activeTextField) { 
    CGRect frame = activeTextField.frame; 
    CGFloat yPoint = scrollView.frame.origin.y+frame.origin.y+frame.size.height+8.0; 
    CGFloat height = self.view.frame.size.height-keyboardHeight; 
    CGFloat diff = yPoint-height; 
    if (diff > 0.0) { 
     [scrollView setContentOffset:CGPointMake(0, diff) animated:YES]; 
    } 
    else { 
     CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y; 
     if (diff<scrollView.frame.size.height) { 
      diff = scrollView.contentSize.height-scrollView.frame.size.height; 
      if (diff < 0) { 
       diff = 0.0; 
      } 
      [scrollView setContentOffset:CGPointMake(0, diff) animated:YES]; 
     } 
    } 
} 
else { 
    CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y; 
    if (diff<scrollView.frame.si ze.height) { 
     diff = scrollView.contentSize.height-scrollView.frame.size.height; 
     if (diff < 0) { 
      diff = 0.0; 
     } 
     [scrollView setContentOffset:CGPointMake(0, diff) animated:YES]; 
    } 
} 
} 

辭職

-(void)resignAllTextFields { 

for (UIView *view in containerView.subviews) { 
    if ([view isKindOfClass:[UITextField class]]) { 
     UITextField *textField = (UITextField*)view; 
     [textField resignFirstResponder]; 
    } 
} 
[self updateScrollViewPosition]; 
}