删除 UITableView 中单元格前的空格

问题描述:

我目前正在尝试将 UITableView 放在不同的位置,而不是放在我的视图控制器的顶部.话虽如此,它正试图在顶部添加标题以说明导航栏,但这不是必需的,因为我的控制器顶部没有.

I am currently trying to put a UITableView in a different location rather than at the top of my view controller. With this said, it is trying to add the header in the top to account for the navigation bar, but this is not needed since I do not have at the top of my controller.

如果我将 UITableView 的左上角放在我想要单元格所在的位置,它不会将单元格放在那里:

If I put the top left corner of the UITableView where I want the cells to be, it doesn't put the cells there:

现在,如果我只是将表格视图向上移动以便单元格位于正确的位置,我会遇到一个不同的问题 - 滚动时单元格可以移动到那里(这是我的手指在导航控制器上方):

Now, if I just move the table view up so the cells are in the correct place, I get a different problem - the cells can be moved up to there when scrolling (this is with my finger above the navigation controller):

当我放手时,是的 - 单元格将位于搜索栏的正下方,但这显然是一个问题,如何将它们置于搜索栏上方.

When I let go, yes - the cells will go right below the search bar, but this is obviously a problem how you can bring them above it.

我该怎么做?有没有更简单的方法?

UITableView 的单元格是否在向下滚动时显示在空白处?

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

如果是这样,那么问题可能是由于您的视图中有导航控制器而添加到 UITableView 的插图.插图被添加到表格视图中,以便在没有发生滚动时将内容放置在导航栏下方.当表格滚动时,内容会滚动并显示在透明导航栏下.这种行为当然只有在表格视图直接在导航栏下方启动时才需要,但这里不是这种情况.

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.

另一件需要注意的是,如果它是 UIScrollView 或其后代(例如 UITableView>UICollectionView).如果您的视图层次结构包含多个滚动视图,automaticallyAdjustsScrollViewInsets 将仅对第一个进行调整.

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) 界面生成器

  • 选择视图控制器
  • 打开属性检查器
  • IB 的属性检查器(选择视图控制器时)有一个名为调整滚动视图插入"的属性,默认情况下处于启用状态.取消选中此选项:

  • 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:


(图片由 Dheeraj D 提供)

我不确定哪个 Xcode 版本引入了这个选项(在发行说明中没有发现它),但它至少在 5.1.1 版本中可用.

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.

为避免混淆,这是评论中提到的第三个选项

b) 以编程方式

将此添加到即 viewDidLoad(归功于 Slavco Petkovski 的回答和 Cris R 的评论)

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) 这可能与老学生有关

您可以通过添加来解决此问题

You can either fix this by adding

tableView.contentInset = UIEdgeInsetsZero

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

或者如果您使用的是 IB 并且导航栏不透明(无法从屏幕截图中看出)

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

  • 选择视图控制器
  • 打开属性检查器
  • 在视图控制器"选项扩展边缘"部分取消选择顶栏下方"