Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the scroll for UITableView

I have a TableViewController:

enter image description here

As you see I have my own custom bar at the top. The UITable View is just a static one, and I add a view at the top of UITableView.

The thing is when I scroll the TableView to top-side it become like bellow image, and I don't want it. is there any easy code that I can limit the scroll for the tableView?

enter image description here

like image 999
Ali Avatar asked Oct 09 '12 17:10

Ali


3 Answers

since UITableView is a subclass of UIScrollView you can use this UIScrollViewDelegate method to forbid scrolling above the top border

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        if (scrollView.contentOffset.y < 0) {
            scrollView.contentOffset = CGPointZero;
        }
    }
}
like image 150
Matthias Bauch Avatar answered Nov 08 '22 23:11

Matthias Bauch


Yo will need to set the bounce property of the uitableview to NO

    UITableView  *tableView;
    tableView.bounces = NO;

Edit: Note also you can uncheck the bounces from interface builder too

Please check this answer for further details Disable UITableView vertical bounces when scrolling

like image 4
Omar Abdelhafith Avatar answered Nov 08 '22 22:11

Omar Abdelhafith


I had the same problem and asked our UX-Designer, how it would be better to do. He said, that both strict solutions (prevent bouncing or allow it as it is) are bad. It's better to allow bouncing but only for some space

My solution was:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == self.tableView {
        if scrollView.contentOffset.y < -64 {
            scrollView.scrollRectToVisible(CGRect(origin: CGPoint(x: 0, y: -64), size: scrollView.frame.size), animated: false)
            scrollView.scrollRectToVisible(CGRect(origin: CGPoint.zero, size: scrollView.frame.size), animated: true)
        }
    }
}

Where 64 was that "some space" for me. Code stops tableView at -64 from the top and brings it up with an animation. Good luck!

like image 2
ramzesenok Avatar answered Nov 08 '22 22:11

ramzesenok