Limit the scroll for UITableView

2019-02-18 06:31发布

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

5条回答
爷、活的狠高调
2楼-- · 2019-02-18 07:06

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!

查看更多
姐就是有狂的资本
3楼-- · 2019-02-18 07:08

You need to create your view controller object as type UIViewController and not UITableViewController. Then add the custom bar as a subview to self.view. Create a separate UITableView and add it below the custom bar. That should make custom bar static and table view scrollable.

Update:

In order to make the tableview static you need to set it as

tableView.scrollEnabled = NO:

Let me know if this works for you.

查看更多
贪生不怕死
4楼-- · 2019-02-18 07:15

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;
        }
    }
}
查看更多
地球回转人心会变
5楼-- · 2019-02-18 07:26

If i understand correctly you have set-up your custom bar as part of your tableview. Put your custom bar in a separate view not in the tableview and put your tableview below custom bar when you are setting up your views. You need to create your custom view controller that will have your custom bar and your static table view.

查看更多
仙女界的扛把子
6楼-- · 2019-02-18 07:30

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

查看更多
登录 后发表回答