OGeek|极客世界-中国程序员成长平台

标题: ios - 在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这一点? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 13:31
标题: ios - 在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这一点?

我需要在我的应用中实现聊天功能。其中我有一个如图所示的 InputView。

图片 1

enter image description here

图片 2

enter image description here .

InputView 是带有左右按钮和 TextView 的 蓝色彩色 View

  1. 单击文本字段时,此输入 View 应向上滚动并且键盘应出现。这个 TextView 是一个不断增长的文本字段,其中包含文本数量。
  2. 但是当我在文本字段中输入多行时。 InputView 和 Textfield 都在增长,但它在键盘下方略微隐藏,如下所示如何克服它???

图 3

enter image description here

**

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;
}



Best Answer-推荐答案


最后我解决了这个问题...在 ScrollView 之后添加了一个 View ,这个技巧奏效了!!! ScrollView --> UIView --> TableView 和 InputView

关于ios - 在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738198/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4