UITearchBar上的UITableView奇怪的偏移问题

时间:2021-12-04 23:02:51

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).

我有一个UITableView,它有一个UISearchBar子视图。这是UIViewController的视图以及一些其他子视图(标签,文本字段等)。

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:

表的搜索栏和内容偏移行为非常奇怪,但它似乎取决于这些视图添加到xib中的主视图的顺序。我创建了一个示例项目,只有我的表/搜索和标签才能测试,结果是一样的。在标签后添加表格时,一切正常:

Setup:

UITearchBar上的UITableView奇怪的偏移问题

Correct and Expected Result:

UITearchBar上的UITableView奇怪的偏移问题

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.

但是,如果我只是改变我的2个子视图位于主视图(也就是在标签之前添加的表)的顺序,那么奇怪的事情就会开始发生。

Apparently bad setup:

UITearchBar上的UITableView奇怪的偏移问题

Weird offset of Search Bar:

UITearchBar上的UITableView奇怪的偏移问题

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.

我没有改变任何其他东西,那么为什么Xcode似乎关心这些子视图添加到主视图的顺序?如果我向上滚动“坏”表设置,搜索栏会立即在其顶部边缘消失,但表的内容将继续向上滚动,直到它到达xib中设置的帧的顶部。向下滚动,搜索栏不会重新出现,直到奇怪的降低位置。这是在Xcode 5.1.1中,而不是新的测试版。打开或关闭Autolayout时结果相同。

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)

知道为什么会这样吗?这是一个错误,还是我错过了什么? (我没有发布任何代码,因为我正在做的是设置部分,行的数量,并在单元格上设置文本。不要搞乱内容插入,偏移,任何东西。我从应用程序委托加载视图导航控制器的根)

4 个解决方案

#1


14  

This happens because a UIViewController's property called automaticallyAdjustsScrollViewInsets

这是因为UIViewController的属性名为automatedAdjustsScrollViewInsets

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).

在iOS 7中,UIViewControllers有一个名为automatedAdjustsScrollViewInsets的属性,默认为YES。如果您有一个滚动视图,它是视图控制器的根视图(例如使用UITableViewController)或索引为0的子视图,那么该属性将调整contentInset和scrollIndicatorInsets。这将允许您的滚动视图启动其内容并滚动导航栏下方的指示器(如果您的视图控制器位于导航控制器中)。

From Big Nerd Ranch

来自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.

默认值为YES,允许视图控制器调整其滚动视图插入以响应状态栏,导航栏和工具栏或标签栏消耗的屏幕区域。如果要自行管理滚动视图插入调整,则设置为NO,例如视图层次结构中有多个滚动视图时。

#2


1  

I have same problem before about position of tableview and searchbar. i tried the following and it works for me.

关于tableview和searchbar的位置之前我有同样的问题。我尝试了以下内容,它对我有用。

If you do not write code for that and if it is only problem of xib or storyboard then try all outlet's autosizing and origin setting to fix its position and see the difference. it may be work for you.

如果你没有为此编写代码,如果它只是xib或storyboard的问题,那么尝试所有outlet的autosizing和origin设置来修复它的位置并查看差异。它可能对你有用。

UITearchBar上的UITableView奇怪的偏移问题

#3


0  

UITableView header can contains only one UIView, so if you need UISearchBar plus UILabel, you need to wrap they into UIView and add this view as UITableView header.

UITableView标头只能包含一个UIView,因此如果需要UISearchBar和UILabel,则需要将它们包装到UIView中并将此视图添加为UITableView标头。

#4


0  

Update : automaticallyAdjustsScrollViewInsets has been deprecated in ios 11 and a new field contentInsetAdjustmentBehavior has been introduced.

更新:在ios 11中已弃用了automaticAdjustsScrollViewInsets,并引入了新的字段contentInsetAdjustmentBehavior。

if #available(iOS 11.0, *) {
    tableview.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}

#1


14  

This happens because a UIViewController's property called automaticallyAdjustsScrollViewInsets

这是因为UIViewController的属性名为automatedAdjustsScrollViewInsets

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).

在iOS 7中,UIViewControllers有一个名为automatedAdjustsScrollViewInsets的属性,默认为YES。如果您有一个滚动视图,它是视图控制器的根视图(例如使用UITableViewController)或索引为0的子视图,那么该属性将调整contentInset和scrollIndicatorInsets。这将允许您的滚动视图启动其内容并滚动导航栏下方的指示器(如果您的视图控制器位于导航控制器中)。

From Big Nerd Ranch

来自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.

默认值为YES,允许视图控制器调整其滚动视图插入以响应状态栏,导航栏和工具栏或标签栏消耗的屏幕区域。如果要自行管理滚动视图插入调整,则设置为NO,例如视图层次结构中有多个滚动视图时。

#2


1  

I have same problem before about position of tableview and searchbar. i tried the following and it works for me.

关于tableview和searchbar的位置之前我有同样的问题。我尝试了以下内容,它对我有用。

If you do not write code for that and if it is only problem of xib or storyboard then try all outlet's autosizing and origin setting to fix its position and see the difference. it may be work for you.

如果你没有为此编写代码,如果它只是xib或storyboard的问题,那么尝试所有outlet的autosizing和origin设置来修复它的位置并查看差异。它可能对你有用。

UITearchBar上的UITableView奇怪的偏移问题

#3


0  

UITableView header can contains only one UIView, so if you need UISearchBar plus UILabel, you need to wrap they into UIView and add this view as UITableView header.

UITableView标头只能包含一个UIView,因此如果需要UISearchBar和UILabel,则需要将它们包装到UIView中并将此视图添加为UITableView标头。

#4


0  

Update : automaticallyAdjustsScrollViewInsets has been deprecated in ios 11 and a new field contentInsetAdjustmentBehavior has been introduced.

更新:在ios 11中已弃用了automaticAdjustsScrollViewInsets,并引入了新的字段contentInsetAdjustmentBehavior。

if #available(iOS 11.0, *) {
    tableview.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}