简体   繁体   中英

`UITableView` extra space at the top

I don't remember seeing this extra space when adding UITableView s 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.

在此输入图像描述

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?

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

在此输入图像描述

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.

如果tableView在tableViewContorller中并且表视图是静态的,则将tableView单元格的样式从“Grouped”更改为“Plain”。这将删除表视图单元格顶部的额外空间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM