Limit the scroll for UITableView

后端 未结 5 827
不知归路
不知归路 2021-01-13 14:27

I have a TableViewController:

\"enter

As you see I have my own custom bar at t

相关标签:
5条回答
  • 2021-01-13 14:41

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-13 14:53

    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.

    0 讨论(0)
  • 2021-01-13 14:58

    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

    0 讨论(0)
  • 2021-01-13 15:00

    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!

    0 讨论(0)
  • 2021-01-13 15:02

    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.

    0 讨论(0)
提交回复
热议问题