UITableView上的UISearchBar奇怪的偏移量问题 [英] UISearchBar on UITableView strange offset issue

查看:238
本文介绍了UITableView上的UISearchBar奇怪的偏移量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其中有一个UISearchBar子视图.所有这些都在UIViewController的视图以及其他一些子视图(标签,文本字段等)上.

表的搜索栏和内容偏移量表现得很奇怪,但似乎取决于这些视图在xib中添加到主视图的顺序.我创建了一个仅带有表/搜索和标签的示例项目以进行测试,结果是相同的.将表添加到标签的 之后,一切正常:

设置:

正确和预期结果:

但是,如果我只是简单地更改两个子视图在主视图上的排列顺序(也就是在标签前 添加了表格),那么奇怪的事情就开始发生了. /p>

显然是错误的设置:

搜索栏的怪异偏移量:

我什么都不会改变,那么为什么Xcode似乎关心这些子视图以什么顺序添加到主视图中?如果在不良"表格设置上向上滚动,搜索栏会立即在其顶部边缘消失,但是表格的内容将继续向上滚动,直到到达xib中设置的框架的顶部.向下滚动,直到奇怪的降低位置,搜索栏才重新出现.这在Xcode 5.1.1中,而不是新的Beta中.无论是否启用自动版式,结果都是一样的.

知道为什么会这样吗?这是一个错误,还是我缺少了什么? (我没有发布任何代码,因为我所做的只是设置单元格的节数,行数和设置文本.不弄乱内容插图,偏移量等.我从应用程序委托中加载视图为导航控制器的根目录

解决方案

之所以会发生这种情况,是因为UIViewController的名为automaticallyAdjustsScrollViewInsets

的属性

在iOS 7中,UIViewControllers具有一个名为的属性 automaticAdjustsScrollViewInsets,默认为YES.如果你 具有滚动视图,该滚动视图可以是视图的根视图 控制器(例如使用UITableViewController)或位于的子视图 索引0,则该属性将同时调整contentInset和 scrollIndicatorInsets.这将允许您的滚动视图开始其 导航栏下方的内容和滚动指示器(如果您查看 控制器位于导航控制器中.

来自大书呆子牧场

如果使用情节提要,则可以通过选择视图控制器对其进行更改,然后在属性检查器中取消选择Adjust scroll view insets.

这是来自

I have a UITableView which has a UISearchBar subview. This is all on the view of a UIViewController along with a few other subviews (labels, text fields and such).

The search bar and content offset of the table are acting quite strangely, but it seems dependent on the order in which these views are added to the main view in the xib. I created a sample project with just my table/search and a label in order to test, and the result is the same. When the table is added after the label, everything works fine:

Setup:

Correct and Expected Result:

However, if I simply change the order in which my 2 subviews sit on the main view (aka table added before the label) then weird things start happening.

Apparently bad setup:

Weird offset of Search Bar:

I'm not changing anything else whatsoever, so why does Xcode seem to care which order these subviews are added to the main view?? If I scroll up on the "bad" table setup, the search bar disappears immediately at its top edge, but the table's content will continue to scroll up until it reaches the top of the frame that was set in the xib. Scroll back down and the search bar doesn't reappear until the strange lowered location. This is in Xcode 5.1.1, not the new beta. The result is the same with or without Autolayout turned on.

Any idea why this is happening? Is this a bug, or am I missing something? (I didn't post any code because all I'm doing is setting the number of sections, rows, and setting the text on the cell. Not messing with content insets, offset, anything. I load the view from the app delegate as the root of a nav controller)

解决方案

This happens because a UIViewController's property called automaticallyAdjustsScrollViewInsets

With iOS 7, UIViewControllers have a property called automaticallyAdjustsScrollViewInsets, and it defaults to YES. If you have a scroll view that is either the root view of your view controller (such as with a UITableViewController) or the subview at index 0, then that property will adjust both the contentInset and the scrollIndicatorInsets. This will allow your scroll view to start its content and scroll indicators below the navigation bar (if your view controller is in a navigation controller).

From Big Nerd Ranch

If you are using storyboards, you can change it by selecting the view controller and in the attributes inspector deselect Adjust scroll view insets.

Here is its description from apple documentation:

Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

这篇关于UITableView上的UISearchBar奇怪的偏移量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆