在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这个?
Keyboard hiding the growing UItextview in Autolayout. How to overcome this?
我需要在我的应用程序中实现聊天功能。在那里我有一个如图所示的 InputView。
Image 1
Image 2
。
InputView 是带有左右按钮和文本视图的 blue Coloured View
。
- 单击文本字段时,此输入视图应该向上滚动并且键盘应该出现。这个文本视图是一个不断增长的文本字段,里面有很多文本。
- 但是当我在文本字段中键入多行时。 InputView 和 Textfield 都在增长,但它稍微隐藏在键盘下面,如下所示如何克服它???
Image 3
**
And I used this code
**.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self deregisterFromKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.inputView.frame.origin) ) {
[self.scrollView scrollRectToVisible:self.inputView.frame animated:YES];
}
[self.view layoutIfNeeded];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view layoutIfNeeded];
}
- (void)textViewDidChange:(UITextView *)textView {
CGSize sizeThatShouldFitTheContent = [textView sizeThatFits:textView.frame.size];
//chatView_heightConstraint is the inputview height
self.chatView_heightConstraint.constant = MIN(sizeThatShouldFitTheContent.height + 8, 87);
CGRect _f = self.textView.frame;
_f.size.height = MIN( self.textView.contentSize.height, 79.0);
self.textView.frame = _f;
}
我终于解决了这个问题...在滚动视图之后添加了一个视图,技巧成功了!!! ScrollView --> UIView --> TableView and InputView
我需要在我的应用程序中实现聊天功能。在那里我有一个如图所示的 InputView。
Image 1
Image 2
InputView 是带有左右按钮和文本视图的 blue Coloured View
。
- 单击文本字段时,此输入视图应该向上滚动并且键盘应该出现。这个文本视图是一个不断增长的文本字段,里面有很多文本。
- 但是当我在文本字段中键入多行时。 InputView 和 Textfield 都在增长,但它稍微隐藏在键盘下面,如下所示如何克服它???
Image 3
**
And I used this code
**.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self deregisterFromKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.inputView.frame.origin) ) {
[self.scrollView scrollRectToVisible:self.inputView.frame animated:YES];
}
[self.view layoutIfNeeded];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view layoutIfNeeded];
}
- (void)textViewDidChange:(UITextView *)textView {
CGSize sizeThatShouldFitTheContent = [textView sizeThatFits:textView.frame.size];
//chatView_heightConstraint is the inputview height
self.chatView_heightConstraint.constant = MIN(sizeThatShouldFitTheContent.height + 8, 87);
CGRect _f = self.textView.frame;
_f.size.height = MIN( self.textView.contentSize.height, 79.0);
self.textView.frame = _f;
}
我终于解决了这个问题...在滚动视图之后添加了一个视图,技巧成功了!!! ScrollView --> UIView --> TableView and InputView