【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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,216評論 0 23
  • 01 去年小長假,去上??磁笥?。飛機上,溫度比較低,準備問空姐要條毛毯。旁邊的漂亮女士見勢,把自己的毛毯給了我。我...
    遇見蓮心閱讀 3,384評論 6 28
  • SAP查看MRP運行結(jié)果的重要事務(wù)碼,MD04,功能比較強大,但是網(wǎng)上缺乏比較詳細的介紹。所以Golden想在這里...
    馬明成閱讀 2,419評論 0 1
  • 大半個月過去了,好久不見,你們還好嗎? 這段時間發(fā)生了很多事,經(jīng)歷了很多心路歷程,有成長,也有感恩。 一直在說初心...
    王子月閱讀 561評論 0 0
  • 2017 年11月19日,晴,星期天。 今天是小兒子入學以來的第一次家長會,早早起床,因為開家長會以前要先給長...
    泰碩麻麻閱讀 279評論 0 3

友情鏈接更多精彩內(nèi)容