5

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

Ali
  • 9,800
  • 19
  • 72
  • 152

6 Answers6

20

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;
        }
    }
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
4

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

Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Is it any possibility to make the scroll like that just when we go bottom to top? because It is better if when we scroll top to bottom it works normally. – Ali Oct 09 '12 at 18:44
  • another possible answer is the one of Mathhias Bunch, it should cover waht you need – Omar Abdelhafith Oct 09 '12 at 18:46
2

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!

ramzesenok
  • 5,469
  • 4
  • 30
  • 41
0

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.

tiguero
  • 11,477
  • 5
  • 43
  • 61
  • You understood right. Then I can't create a static table view in a tableViewController. – Ali Oct 09 '12 at 18:13
  • You need to create you own view controller which will contain a table view and the custom bar. If you have define a table view with some predefined contents you can link to it in storyboard – tiguero Oct 09 '12 at 18:18
  • I tried it in a .xib file, and there I did not have an option to set the content of TableView to static. So it means I have to create a dynamic Table View. right? – Ali Oct 09 '12 at 18:24
  • How could you set the content of tableView to static in a .xib file? – Ali Oct 09 '12 at 18:26
  • you can go to your attributes inspector and change the contents from dynamic prototypes to static cells – tiguero Oct 09 '12 at 18:28
  • I said the `.xib file`. I did not have an option to set the content to static. I think you are not right in this case. – Ali Oct 09 '12 at 18:31
  • Create a simple .xib file and then drag a UITableView in it. See the properties. Can you set the content of tableView to static? there is no option. – Ali Oct 09 '12 at 18:34
  • http://stackoverflow.com/questions/8953490/no-content-attribute-for-uitableview-in-xib – Ali Oct 09 '12 at 18:37
  • again...if you want ur content to be static u should set the content properties of your table view in interface builder to static cells and edit ur cell it is not an option on the xib. – tiguero Oct 09 '12 at 19:16
0

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.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • Yes, but then the tableView can not be static. right? I tried it in a .xib file, and there I did not have an option to set the `content` of TableView to static. – Ali Oct 09 '12 at 18:23
  • I have edited my answer. Please let me know if that is what you wanted. – iDev Oct 09 '12 at 18:39
0

Swift version of Mattias's answer:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (scrollView == self.ordersTable) {
            if (scrollView.contentOffset.y < 0) {
                scrollView.contentOffset = CGPoint.zero;
            }
        }
    }