简体   繁体   English

滚动UIView和UITableView滚动

[英]Scrolling a UIView along with UITableView scroll

I have a tableview in which UITableViewCell contains a textfield. 我有一个tableview,其中UITableViewCell包含一个文本字段。 When user tap on the textfield and keyboard appears, I scroll to the tapped in row after setting proper content inset. 当用户点击文本字段并出现键盘时,我会在设置正确的内容插入后滚动到点击的行。

Now, I have a UIView shown on top of my table view which I need to adjust accordingly when table scrolls. 现在,我在桌面视图的顶部显示了一个UIView,当桌面滚动时我需要相应地调整。

To do this, I implemented scrollViewDidScroll: method as below. 为此,我实现了scrollViewDidScroll:方法,如下所示。 While this works in certain situations, it do not work or give random results in some situations. 虽然这在某些情况下有效,但在某些情况下它不起作用或给出随机结果。 Am I doing something wrong here? 我在这里做错了吗?

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

you can use this for scrolling table in ios. 您可以使用它来在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];       
       }

}

If you want to scroll the both view simultaneously then you should have to take both views with same size and in following method just set the scrollview content offset to them. 如果要同时滚动两个视图,则必须同时使用相同大小的两个视图,并且在以下方法中只需将scrollview内容偏移设置为它们。 It will surly work 这将是非常有效的

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

//Note :- Make sure view should be scrollview type or if view is getting scared or shows black screen //注意: - 确保视图应为滚动视图类型,或者视图是否受到惊吓或显示黑屏

Thanks.. 谢谢..

Use this library: 使用此库:

https://github.com/hackiftekhar/IQKeyboardManager https://github.com/hackiftekhar/IQKeyboardManager

It will handle the position of Views when tapped on a textfield. 它将在文本字段上轻触时处理视图的位置。

So, this piece worked for me for anyone who is struggling with same situation: 所以,对于那些在同样情况下挣扎的人来说,这件作品对我有用:

- (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