How can I prevent UITableViewRowAnimationAutomatic to slide down my UISearchBar?

  • I have figured out how to hide the SearchBar when my view appears, so it will be hidden unless you scrolled it down and only used modal dialogues. However, if I add or delete a row from my table, the search bar slides into the visible part of the view, due to the animation triggered. Is it possible to disallow the animation to scroll the view down if that would cross a certain height?

    -- Florian
  • 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 an stackoverflow question: http://stackoverflow.com/questions/664781/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).

    Am Donnerstag, 19 April 2012 um 08:11 schrieb Florian Pilz:

    > I have figured out how to hide the SearchBar when my view appears, so it will be hidden unless you scrolled it down and only used modal dialogues. However, if I add or delete a row from my table, the search bar slides into the visible part of the view, due to the animation triggered. Is it possible to disallow the animation to scroll the view down if that would cross a certain height?
    >
    > -- Florian
previous month april 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today