[iphone] How to disable scrolling in UITableView table when the content fits on the screen

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 from UITableViewController) 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 in Interface Builder) displayed on the screen, whereas table.contentSize.height is the heights of: the header, the footer, and the height of every cell added together.

This question is related to iphone ios uitableview scroll

The answer is


In Swift:

tableView.alwaysBounceVertical = false

// Enable scrolling based on content height self.tableView.scrollEnabled = table.contentSize.height > table.frame.size.height;


The default height is 44.0f for a tableview cell I believe. You must be having your datasource in hand in a Array? Then just check if [array count]*44.0f goes beyond the frame bounds and if so set tableview.scrollEnabled = NO, else set it to YES. Do it where you figure the datasource out for that particular tableview.


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.

You can set enable/disable bounce or scrolling the tableview by selecting/deselecting these in the Scroll View area

Scroll View editing area


I think you want to set

tableView.alwaysBounceVertical = NO;

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:

enter image description here

If you're not using AutoLayout:

 override func viewDidLayoutSubviews() {
    // Enable scrolling based on content height
    tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
 }

You can edit this in your storyboard (if you are using one). Under the table view there is a checkbox that says "Scrolling Enabled". Uncheck it and you're done.


try this

[yourTableView setBounces:NO];

Examples related to iphone

Detect if the device is iPhone X Xcode 8 shows error that provisioning profile doesn't include signing certificate Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone Certificate has either expired or has been revoked Missing Compliance in Status when I add built for internal testing in Test Flight.How to solve? cordova run with ios error .. Error code 65 for command: xcodebuild with args: "Could not find Developer Disk Image" Reason: no suitable image found iPad Multitasking support requires these orientations How to insert new cell into UITableView in Swift

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to uitableview

Adding a UISegmentedControl to UITableView make UITableViewCell selectable only while editing How to fix Error: this class is not key value coding-compliant for the key tableView.' UITableView example for Swift Swift - how to make custom header for UITableView? How to insert new cell into UITableView in Swift How to detect tableView cell touched or clicked in swift Dynamic Height Issue for UITableView Cells (Swift) UIButton action in table view cell How to get the indexpath.row when an element is activated?

Examples related to scroll

How to window.scrollTo() with a smooth effect Angular 2 Scroll to bottom (Chat style) Scroll to the top of the page after render in react.js Get div's offsetTop positions in React RecyclerView - How to smooth scroll to top of item on a certain position? Detecting scroll direction How to disable RecyclerView scrolling? How can I scroll a div to be visible in ReactJS? Make a nav bar stick Disable Scrolling on Body