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.
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.
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.
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.
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
网友评论