How to disable scrolling in UITableView table when the content fits on the screen
Solution 1
I think you want to set
tableView.alwaysBounceVertical = NO;
Solution 2
In Swift:
tableView.alwaysBounceVertical = false
Solution 3
You can verify the number of visible cells using this function:
- (NSArray *)visibleCells
This method will return an array with the cells that are visible, so you can count the number of objects in this array and compare with the number of objects in your table.. if it's equal.. you can disable the scrolling using:
tableView.scrollEnabled = NO;
As @Ginny mentioned.. we would can have problems with partially visible cells, so this solution works better in this case:
tableView.scrollEnabled = (tableView.contentSize.height <= CGRectGetHeight(tableView.frame));
In case you are using autoLayout this solution do the job:
tableView.alwaysBounceVertical = NO.
Solution 4
So there's are multiple answers and requires a all content at once place so I'm adding this answer:
If you're using AutoLayout, by setting this only should work for you:
- In code:
tableView.alwaysBounceVertical = false
- or In Interface Builder:
Just find this option and untick
"Bounce Vertically
" option.
Here's the reference:
If you're not using AutoLayout:
override func viewDidLayoutSubviews() {
// Enable scrolling based on content height
tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
}
Solution 5
try this
[yourTableView setBounces:NO];
Related videos on Youtube
Ginny
N00b iphone developer, actually n00b developer in general. Still learning!
Updated on January 18, 2020Comments
-
Ginny over 4 years
I have a few (grouped style) tables in my iphone app (only on part of the screen and added with
Interface Builder
though, not subclassed fromUITableViewController
) that 80% of the time are small and will fit on the screen. When the table fits on the screen, I'd like to disable scrolling, to make it a bit cleaner. But if the table goes off the screen (when rows are later added to it), I'd like to enable scrolling again (because otherwise you can't see that content.)Is there a way to do this? I can't seem to figure it out. I do know to do:
tableView.scrollEnabled = NO;
but I'm not sure where, or if I have to calculate the table object size or something to get this to work.
Update: Here's the solution that finally worked for me:if (table.contentSize.height < table.frame.size.height) { table.scrollEnabled = NO; } else { table.scrollEnabled = YES; }
I run this code after calling
reloadData
on the table, and it calculates the right sizes and appears to work.table.frame.size.height
is the actual size of the object (you can see this inInterface Builder
) displayed on the screen, whereastable.contentSize.height
is the heights of: the header, the footer, and the height of every cell added together.-
Brandon O'Rourke over 12 yearsThanks. That solution worked for me. However, I ended up extending UITableView and overriding reloadData to disable scrollEnabled. The reason is that the table view is loaded when my view controller is created. That way you don't have to call reloadData twice.
-
Kyle Clegg over 9 years@JoeBlow I think you missed OP's point. The question isn't how to disable bounce, it's how to only enable scrolling (and bounce, most likely) when the tableview needs to scroll.
-
Ricardo over 8 yearsMaybe is better to use bounds, because I think if you rotate a bit your table, frame.height is going to change.
-
Grzegorz Krukowski about 8 yearsThere should be: if (table.contentSize.height <= table.frame.size.height)
-
-
Ginny over 12 yearsMy table cells are different sizes (and may change size at any time; I'm using the heightForRowAtIndexPath method) so I probably wouldn't want to hardcode 44.0f. I believe that number is the same as tableView.rowHeight anyway?
-
Bourne over 12 yearsYou would then be doing your total height calculation in that method. Simple right? If that exceeds screen bound, enable scroll, else leave it disabled.The method would be called for all the visible cells. If you don't have enough data to exceed screen, you can calculate the total height right in that method. Keep adding to a global variable the height it returns on each call and you have your total height.
-
Bourne over 12 yearsRoberto's answer is an easier way of achieving all this and leaving the calculation to the tableview.
-
Ginny over 12 yearsThank you! This is mostly working for me now. Only problem I'm having now is when cells are partially visible they still count as visible and thus scrolling gets disabled when in that case I want it to be enabled.
-
Ginny over 12 yearsFigured it out! Turns out you can't really use visibleCells at all, because of the partially visible cell problem, but this works:
if (table.contentSize.height < table.frame.size.height)
>> see edit in the original post. -
Joshua Dance over 10 yearsThis works better than contentSize < frame when you are using AutoLayout. Should be accepted answer.
-
Fattie over 9 years@kyle -- thanks for the note! -- but look that's what happens automatically. If it is shorter than the screen (or other holder), it does NOT scroll (and obviously won't bounce, coz it won't even scroll). If it is longer than the screen, it will then scroll (and indeed bounce). No??
-
Fattie over 9 yearsMaybe I've been using collection views too much ... maybe it works as I describe for collection views, but not for UITableView??
-
Golden Thumb over 9 yearsI'm using auto layout and this answer is exactly what I need.
-
Kirit Vaghela almost 9 yearsbetter than disabling scrolling
-
Ahsan Ebrahim almost 9 yearsworks like a charm, you can also disable it from the
interface builder
by unticking bounce and bounce horizontally/vertically. -
swiftBoy about 8 yearsfor Swift - tableView.alwaysBounceVertical = false
-
Frank almost 8 yearsThere is a checkbox in attribute inspector. You can uncheck instead of writing code
-
Lope almost 8 years@Feng this checkbox doesn't seem to be working properly (or at all), I had to set it in code to make it work
-
Dmitry Isaev over 6 yearsI always upvote such a̶n̶s̶w̶e̶r̶s̶ "rewrites" because converting to modern syntax requires time.
-
Arash over 6 yearsI think I was enlightened after reading your solution. Many thanks
-
Maria about 6 yearsI used this in my viewDidAppear
-
Maria about 6 yearsI meant viewDidLayoutSubviews not viewDidAppear
-
blackjacx over 2 yearsDude this solved one of the nastiest bugs we had in our app. What the heck did apple think when giving this property it's name 😅!?!?