Morgan Wilde
Morgan Wilde

Reputation: 17303

`UITableView` extra space at the top

I don't remember seeing this extra space when adding UITableViews to my scenes. This happened when I dragged the default object from the object library in Interface Builder.

To highlight the gap, I've changed the background color for the table to grey.

enter image description here

I fill the table with data with the following code.

extension UserListViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)
        cell.textLabel?.text = users[indexPath.row].name
        return cell
    }
}

And I set the dataSource for the outline inside my viewDidLoad of the view controller.

tableView.dataSource = self

So where does this gap come from? And how do I get rid of it?

Upvotes: 3

Views: 4307

Answers (3)

Nancy
Nancy

Reputation: 178

If the tableView is inside a tableViewContorller and the table view is static,then change the style of the tableView cells from "Grouped" to "Plain".This would remove extra space at the top of the table view cells.

Upvotes: 1

nicky_1525
nicky_1525

Reputation: 959

You can also deselect it directly from the Attribute inspector of your UIViewController in the StoryBoard, so you don't add code.

enter image description here

Upvotes: 13

streem
streem

Reputation: 9144

You can try to set this in your controller :

self.automaticallyAdjustsScrollViewInsets = false

This property is quite self explanatory. With in iOS 7, Apple introduced a translucent top bar. Usually they expect that your UITableview starts from the top like some part is behind the top bar. In this case it is useful to have an automatic inset at the top so that the content is visible.

Upvotes: 5

Related Questions