iOS 7 Uitableview: How to Remove Space Between Navigation Bar and First Cell

iOS 7 UITableView: How to remove space between navigation bar and first cell

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;

Spacing between navigationBar and tableview cell

You need to add contentInset to your UITableView the first value is for top inset the second is left third for bottom and the last for right

Like this

self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0) //replace 10 by your needed value

Remove empty space before cells in UITableView

Do the cells of the UITableView show on the empty space when you scroll down?

If so, then the problem might be the inset that is added to the UITableView because of the Navigation controller you have in your view.
The inset is added to the table view in order for the content to be placed below the navigation bar when no scrolling has occurred. When the table is scrolled, the content scrolls and shows under a transparent navigation bar. This behavior is of course wanted only if the table view starts directly under the navigation bar, which is not the case here.

Another thing to note is that iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

Here's how to change this behavior:

a) Interface Builder

  • Select the view controller
  • Open Attributes inspector
  • There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a view controller is selected) which is on by default. Uncheck this option:


    (Image courtesy of Dheeraj D)

I'm not sure which Xcode version introduced this option (didn't spot it in the release notes), but it's at least available in version 5.1.1.

Edit: To avoid confusion, this was the third option mentioned in the comments

b) Programmatically

Add this to i.e. viewDidLoad (credits to Slavco Petkovski's answer and Cris R's comment)

// Objective-C
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c) This might be relevant for old schoolers

You can either fix this by adding

tableView.contentInset = UIEdgeInsetsZero

//Swift 3 Change
tableView.contentInset = UIEdgeInsets.zero

Or if you are using IB and if the navigation bar is not transparent (can't tell from the screenshot)

  • Select the view controller
  • Open Attributes inspector
  • In View Controller options Extend Edges section deselect "Under Top Bars"

remove top margin between uiview and uitableview

In addition to the previous answer. You need to remove the headerView from your tableView and call layoutIfNeeded() to apply the change and remove the blank space:

yourtableView.tableHeaderView?.removeFromSuperview()
yourtableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
yourtableView.layoutIfNeeded()

Swift: TableViewController in NavigationController - Change distance between first static cell and top

That top space comes from grouped style tableView header. You can add your own table header view at top of the table, set its color to "Group Table View Background Color", and set its height to what you want (16).

enter image description here



Related Topics



Leave a reply



Submit