我需要在我的应用中实现聊天功能。其中我有一个如图所示的 InputView。
图片 1
图片 2
InputView 是带有左右按钮和 TextView 的 蓝色彩色 View
。
图 3
**
And I used this code
**.
-(void)viewWillAppearBOOL)animated{
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)viewWillDisappearBOOL)animated {
[super viewWillDisappear:animated];
[self deregisterFromKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardWasShown
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardWillBeHidden
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardWillBeHidden
name:UIKeyboardDidHideNotification object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShownNSNotification*)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)keyboardWillBeHiddenNSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view layoutIfNeeded];
}
- (void)textViewDidChangeUITextView *)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 之后添加了一个 View ,这个技巧奏效了!!! ScrollView --> UIView --> TableView 和 InputView
关于ios - 在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738198/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |