【iOS】如何移除UITableView顶部的空白

Almost every time I lay down a navigation controller, with a root view controller that contains a table view the result is a table view with an unexpected padding on it’s header.

Why

The reason this happens isUIViewController’s propertyautomaticallyAdjustsScrollViewInsets, which isa Boolean value that indicates whether the view controller should automatically adjust its scroll view insets., and defaults toYES.

The property has been introduced in iOS 7 and together withedgesForExtendedLayoutmakes the nice effect of content scrolling below a translucent navigation bar possible.

How to fix it

So far I found two ways to solve this UI issue:

SetautomaticallyAdjustsScrollViewInsetstofalse

Setting theautomaticallyAdjustsScrollViewInsetsvalue tofalsesolves the issue.

In Interface Builder this can be done by unticking the “Adjust Scroll View Inset” option of the view controller where the table view is.

Make the navigation bar non translucent

Another option is to make the navigation bar non translucent. Such a navigation bar doesn’t trigger the scroll view insets adjustments in the view controller.

In Interface Builder this can be done by unticking the “Translucent” option of the navigation controller’s navigation bar. I also found that the only way to actually select that component is through the view hierarchy tree on the left.

UIViewControllercontainment

A final note on table views embedded via view controller containment. To avoid the header in those the we need again to set tofalsetheautomaticallyAdjustsScrollViewInsetsproperty of theparentview controller.

This seemed counter intuitive to me at first. But if we think about how the containment process works, by extracting the view from the child and placing it in the parent, than it becomes obvious that the layout rules such asautomaticallyAdjustsScrollViewInsetsof the parent applies to the child as well.

Food for thoughts

Are there other, maybe better, way to fix this issue?

Can it be avoided by laying down the views differently?

Why the standard navigation controller + table view controller from the Interface Builder components library doesn’t do this?

Look deeper at theautomaticallyAdjustsScrollViewInsetsandedgesForExtendedLayoutmechanics.

原文链接:http://mokagio.github.io/tech-journal/2015/01/23/ios-unexpected-table-view-header-padding.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,493评论 0 23
  • 01 去年小长假,去上海看朋友。飞机上,温度比较低,准备问空姐要条毛毯。旁边的漂亮女士见势,把自己的毛毯给了我。我...
    遇见莲心阅读 8,538评论 6 28
  • SAP查看MRP运行结果的重要事务码,MD04,功能比较强大,但是网上缺乏比较详细的介绍。所以Golden想在这里...
    马明成阅读 6,872评论 0 1
  • 大半个月过去了,好久不见,你们还好吗? 这段时间发生了很多事,经历了很多心路历程,有成长,也有感恩。 一直在说初心...
    王子月阅读 3,280评论 0 0
  • 2017 年11月19日,晴,星期天。 今天是小儿子入学以来的第一次家长会,早早起床,因为开家长会以前要先给长...
    泰硕麻麻阅读 1,670评论 0 3

友情链接更多精彩内容