简体   繁体   中英

Scrolling a UIView along with UITableView scroll

I have a tableview in which UITableViewCell contains a textfield. 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.

To do this, I implemented scrollViewDidScroll: method as below. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM