我有一個UIView
其中加入作爲子視圖UIScrollView
的UIView
包含UITextField
和UITextView
爲了避免鍵盤隱藏我已經註冊爲keyboardWasShown
和keyboardWillHide
通知的字段和我有這片編寫的代碼滾動的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;
}
這裏activeTextField
和activeTextView
是目前使用文本字段這部作品很好在大多數情況下,但在少數失敗可以有人建議,如果有什麼不對的實施。
使用本https://github.com/hackiftekhar/IQKeyboardManager – karthikeyan
我不打算使用任何第三方框架。 –
您是否將委託設置爲所有UITextFields? –