Since UITableView
is a subclass of UIScrollView
:
- (void)viewDidLoad {
[super viewDidLoad];
// mySubview is an instance variable, declared in .h file
[self.tableView addSubview:mySubview];
// here goes the rest of your code
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(scrollView == self.tableView) {
mySubview.frame = CGRectMake(mySubview.frame.origin.x, scrollView.contentOffset.y, mySubview.frame.size.width, mySubview.frame.size.height);
}
}
The code was taken from WWDC '10 or '11 (I don't remember), so I'm sure it's the most appropriate way to do it.
Explanation: In -viewDidLoad
you create your view and add it as a subview of your tableView. You can do it in -loadView
or -init
- it doesn't matter. The most important lines are in the -scrollViewDidScroll:
method. This method is called whenever user drags the scrollView, so you can simply set the origin.y
of your subview to contentOffset.y
of the scrollView.