【问题标题】:Autoscrolling tableview content based on keyboard appearance根据键盘外观自动滚动 tableview 内容
【发布时间】:2018-09-05 13:48:30
【问题描述】:

在我的主视图控制器中,我展示了一个弹出窗口,它是一个 UITableviewcontroller 类,并且有一个可调整大小的文本视图作为单元格之一。现在随着内容的增长和文本视图的扩展,输入内容超出了键盘,并且在屏幕上不可见。为了解决这个问题,我计算了光标位置和键盘位置,并在此基础上调整了 tableviews 内容偏移量,以便在开始输入时调整偏移量以在键盘上方显示输入内容。它似乎有效,但根据我的逻辑,现在的问题是当有大量内容并且光标位于底部并且如果您滚动回顶部并在光标保持在底部时开始输入,它不会滚动到那里马上,因为我刚刚将 20pt 的空间调整为内容偏移。我不确定如何根据光标点计算 tableview 的内容偏移量。以下是我到目前为止的代码。任何帮助表示赞赏。

-(void)adjustTextScroll:(UITextView *)textView
{
    UITextRange *selectedTextRange = textView.selectedTextRange;
    CGRect windowRect = CGRectZero;
    if (selectedTextRange != nil)
    {
        CGRect caretRect = [textView caretRectForPosition:selectedTextRange.end];

        windowRect = [textView convertRect:caretRect toView:nil]; 

    }


    //Checks if current cursor position is behind keyboard position
    if (CGRectGetMinY(windowRect) > (keyboardYpos - 50)) // 50 added for space difference margin from keyboard
    {

        CGPoint contentOffset = self.tableView.contentOffset;
        contentOffset.y += 20 ;
        self.tableView.contentOffset = contentOffset;

    }

}


//Keyboard notification 
- (void)keyboardWasShown:(NSNotification *)notification
{

    // Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGFloat height = MIN(keyboardSize.height,keyboardSize.width);
    CGFloat mainViewHeight = MAX([[UIScreen mainScreen]bounds].size.width,[[UIScreen mainScreen] bounds].size.height);

    keyboardYpos = mainViewHeight - height;

}

- (void)textViewDidChange:(UITextView *)textView{
    [self adjustTextScroll:textView]
}

【问题讨论】:

    标签: ios uitableview xcode9 uikeyboard caret


    【解决方案1】:

    使用TPKeyboardAvoiding 并让它处理所有滚动问题。我自己已经开发了很多使用它的应用程序。像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多