簡體   English   中英

滾動UIView和UITableView滾動

[英]Scrolling a UIView along with UITableView scroll

我有一個tableview,其中UITableViewCell包含一個文本字段。 當用戶點擊文本字段並出現鍵盤時,我會在設置正確的內容插入后滾動到點擊的行。

現在,我在桌面視圖的頂部顯示了一個UIView,當桌面滾動時我需要相應地調整。

為此,我實現了scrollViewDidScroll:方法,如下所示。 雖然這在某些情況下有效,但在某些情況下它不起作用或給出隨機結果。 我在這里做錯了嗎?

- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
    if (self.hollowView) {
        CGPoint offset = iScrollView.contentOffset;
        CGRect hollowViewFrame = self.hollowView.hollowFrame;
        hollowViewFrame.origin.y += self.previousOffset - offset.y;
        self.previousOffset = offset.y;
        [self.hollowView resetToFrame:hollowViewFrame];
    }
} 

- (void)keyboardDidHide:(NSNotification *)iNotification {
    self.previousOffset = 0.0;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

您可以使用它來在ios中滾動表格。

-(void )keyboardWillShow:(NSNotification *)notification
 {

    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin =[keyboardInfovalueForKey:UIKeyboardFrameEndUserInfoKey];
    keyBoardHeight = keyboardFrameBegin.CGRectValue.size.height;
    vwSendBottomSpaceContraint.constant = keyBoardHeight
    [self performSelector:@selector(scrollToBottomAnimated) withObject:nil  afterDelay:0.1];

}

-(void)keyboardWillHide:(NSNotification*)notification
{

      vwSendBottomSpaceContraint.constant = 0;
      [self performSelector:@selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];

}

-(void)scrollToBottomAnimated
{

       NSInteger rows = [tableview numberOfRowsInSection:0];

       if(rows > 0)
       {
         [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]                            atScrollPosition:UITableViewScrollPositionBottomanimated:YES];       
       }

}

如果要同時滾動兩個視圖,則必須同時使用相同大小的兩個視圖,並且在以下方法中只需將scrollview內容偏移設置為它們。 這將是非常有效的

enter code here - (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.scrollView) // Check for the scrolled view 
{
    // Set the Tableview offset directly
  tableview.contentOffset = iScrollView.contentOffset  
}
else {
   //Set view scroll as per tableview scroll
       view.contentOffset = iScrollView.contentOffset   } }

//注意: - 確保視圖應為滾動視圖類型,或者視圖是否受到驚嚇或顯示黑屏

謝謝..

使用此庫:

https://github.com/hackiftekhar/IQKeyboardManager

它將在文本字段上輕觸時處理視圖的位置。

所以,對於那些在同樣情況下掙扎的人來說,這件作品對我有用:

- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
    if (self.hollowView) {
        CGRect frame = [self.tableView rectForRowAtIndexPath:self.expandedCellIndexPath];
        frame.origin.y -= self.tableView.contentOffset.y;
        [self.hollowView resetToFrame:frame];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM