0

I have a UIViewController - WSProfileViewController. This VC has a headerView, and then a containerView that loads one of three tableViewController as a childViewController of WSProfileViewController. I want to be able to scroll the headerView up whenever the current tableView scrolls up, as if it was a headerView of the tableView itself.

In the main VC, WSProfileViewController, I made it a UIScrollViewDelegate and implemented scrollViewDidScroll, but it's never called. It currently has no connection the the scrollViews of the child tableViews.

*Each child tableViewController has different data models, and are classes used elsewhere, so I want to keep them as is and just load them as children.

OdieO
  • 6,836
  • 7
  • 56
  • 88

1 Answers1

1

So I didn't want to alter any of the code for the child view controllers but I'm pretty sure there isn't any way for the parent to directly capture child scrolling unless I make it the delegate of the child tableviews, but I can't do that because each child view controller needs to be their own tableview's delegate.

Correct me if I'm missing something.

What I did was send a notification from each child's scrollViewDidScroll method like so:

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    NSDictionary *scrollViewInfo = [NSDictionary dictionaryWithObject:scrollView  forKey:@"someKey"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"someNotificationName" object:nil userInfo:scrollViewInfo];

}

And then simply observe that in the parent:

#pragma mark - Notifications
- (void)registerForNotifications {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(childScrollViewDidScroll:) name:@"someNotificationName" object:nil];
}

- (void)childScrollViewDidScroll:(NSNotification *)note {

    UIScrollView* scrollView = [[note userInfo] valueForKey:@"someKey"];

    CGFloat scrollOffset = scrollView.contentOffset.y;    

    CGRect headerFrame = self.headerView.frame;

    headerFrame.origin.y = -scrollOffset;

    self.headerView.frame = headerFrame;

}

It's scrolling the header view on the device without lag, so at least for the moment I'm satisfied.

OdieO
  • 6,836
  • 7
  • 56
  • 88
  • I don't think that this will perform that fine-grainly. As when the users scrolls rapidly, you can't expect notification to be observed as smoothly as the scroll. – Kamala Dash Apr 19 '16 at 12:51