I have an UISearchBar in the header view of my table. Whenever an animation occurs, e.g. when deleting or inserting rows, the header view scrolls down. Is there a way to tell the table view, that it should not (automatically) scroll down the header view? (Only the user should be able to slide it down.)
Asked
Active
Viewed 703 times
1 Answers
0
After an extensive search on Google & StackOverFlow I found the right clue: the UIScrollView
. The actual solution I was looking for and finally implemented can be found in another question: Change Default Scrolling Behavior of UITableView Section Header
I customized the solution a little bit, to have the following properties:
- row animations may scroll down the table view, unless the header view would be shown
- a click on the top ("scroll to top") should scroll the table view to the top, excluding the header view
- if the header view is partially visible it should slide itself down, so its completely visible
Code for this solution:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat searchBarHeight = self.searchBar.frame.size.height;
if (scrollView.contentOffset.y >= searchBarHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-searchBarHeight, 0, 0, 0);
} else {
scrollView.contentInset = UIEdgeInsetsZero;
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (scrollView.contentOffset.y < self.searchBar.frame.size.height && scrollView.contentOffset.y > 0) {
[scrollView setContentOffset:CGPointZero animated:YES];
}
}
This code is implemented inside a UITableViewController
, which acts as a delegate for the UITableView
. The UITableView
on the other side inherits from UIScrollView
, which makes these methods available and performs all the magic.
A few sentences to explain contentOffset
and contentInset
, which took me a while to understand their meaning.
- The offset is a
CGPoint
, which simply tells how far the visible content, i.e. the sections & cells of the table view, are scrolled down from the top left corner. The header view and footer view are included, therefore an offset of (0, 0) says that the table view is scrolled to the top. An offset of (0, 100) means the table view is scrolled down 100px (the x-value of the point is normally not used). - The inset is a very interesting property, it defines the boundaries of the
UIScrollView
as the quadrupel(top, left, bottom, right)
. Therefore the table view will bounce if you cross these boundaries. The default value is (0, 0, 0, 0), meaning that the table view will bounce, if you scroll above / below its first / last cell. Therefore a value of (-44, 0, 0, 0) would lower the bounce-boundarie to 44px below the origin of the table view. As my header view is 44px high, it will bounce every time you scroll the header view into the visible part of the screen. This also prevent row animations from sliding the table below an offset of (44, 0) and the "slide to top" command will slide the table view to an offset of (44, 0).
I hope that helps. :)

Community
- 1
- 1

Florian Pilz
- 8,002
- 4
- 22
- 30