UITableView 滑動問題

緒論

在我們的關于UItableView的計算過程中,總會有很多的問題出現的。UITableView 的三大屬性contentSize、 contentOffset、contentInset也是隨著其自身的大小而不斷變化。其實概念是很好區分的,但是很多時候你會發現contentSize、contentOffset對于高度計算會造成很大的困擾,使自己需要計算的高度出現偏差。

概念

  • contentSize 是tableView可以滾動的區域,就以現在的蘋果6為例,此時的frame = (0 ,0 ,375,667) contentSize = (375 ,1000),代表tableView可以上下滾動,滾動區域比frame大,此時超過了約束的大小就可以自由的滑動,因此也有了偏移量。
  • contentOffset 是tableView當前顯示區域頂點相對于frame頂點的偏移量(向屏幕內拉,偏移量是負值。向屏幕外推,偏移量是正數。)簡言之:向上滑動偏移量為正,向下滑動偏移量為負。,比如上個例子,從初始狀態向下拉10像素,contentoffset就是(0 ,-10),從初始狀態向上推tableview10像素,contentOffset就是(0 ,10)。
  • contentInset 是tableView的contentview的頂點相對于scrollview的位置,例如你的contentInset = (0 ,100),那么你的contentview就是從scrollview的(0 ,100)開始顯示.(這個比較簡單理解)

具體事例

就近期修改應用備注描述界面遇到的問題。當鍵盤彈出的時候,tableView需要適當的移動來防止輸入框不被鍵盤遮擋。以下就是解決思路和具體實現流程。
對于這個界面,首先我們每次都要時時刻刻的去計算tableView和textView的高度問題。所以我們在解決這方面的問題要全面考慮問題;此時可以分為兩個方面去討論問題:

contentSize 小于屏幕高度

此刻我們就在計算的時候調用UItextView的通知方法,當contentSize的大小發生變化的時候去計算tableViewSize,因為此時并沒有偏移量contentoffset,所以不會影響我們計算的高度,可以按照正常的邏輯進行計算。

屏幕快照 2017-04-14 上午11.37.12.png
實現代碼代碼如下:
    CGFloat keyboardHeight = self.currentKeyboardHeight;
    UIView *textField = self.currentEditingField.superview;
    if (keyboardHeight == 0 || textField == nil) {
        return;
    }
    NSLog(@"tableview%f",self.tableView.frame.origin.y);
    float bottomHeight = 0;
    float top          = 0;
    CGRect currentRect = [self.view convertRect:textField.frame fromView:textField];
        bottomHeight = self.view.frame.size.height - (currentRect.origin.y + currentRect.size.height);
        top = self.tableView.frame.origin.y - (keyboardHeight - bottomHeight + 5);
        if (keyboardHeight > bottomHeight) {
            [UIView animateWithDuration:self.currentdurationTime animations:^{
                [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.offset(top);
                }];
                [self.view layoutIfNeeded];
            }];
        }

contentSize 大于屏幕高度

情景1

  • contentSize超過屏幕高度并且具有偏移量textView沒有超出屏幕
屏幕快照 2017-04-14 上午11.48.19.png

情景2

  • contentSize超過屏幕高度沒有偏移量textView沒有超出屏幕
屏幕快照 2017-04-14 上午11.50.32.png

情景3

  • contentSize超過屏幕高度沒有偏移量textView超出屏幕
屏幕快照 2017-04-14 上午11.54.11.png
以上三種情況都要去計算和考慮具體實現代碼如下:
CGFloat keyboardHeight = self.currentKeyboardHeight;
    UIView *textField = self.currentEditingField.superview;
    if (keyboardHeight == 0 || textField == nil) {
        return;
    }
    NSLog(@"tableview%f",self.tableView.frame.origin.y);
    float bottomHeight = 0;
    float top          = 0;
    CGRect currentRect = [self.view convertRect:textField.frame fromView:textField];
bottomHeight =  currentRect.size.height - (self.view.frame.size.height - currentRect.origin.y);
        if ([self.currentEditingField isKindOfClass:[UITextField class] ]) {
            top = self.tableView.frame.origin.y - (keyboardHeight + bottomHeight + 5);
        }else{
            top = self.tableView.frame.origin.y - (keyboardHeight + bottomHeight + 5) - self.tableView.contentOffset.y - 64;
        }
        
        [UIView animateWithDuration:self.currentdurationTime animations:^{
            [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                if (top  <= 0) {
                    make.top.offset(top);
                }
            }];
            [self.view layoutIfNeeded];
        }];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容