本文介绍了嵌入导航栏中时,iOS 7 中奇怪的 UISearchDisplayController 视图偏移行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在构建一个仅限 iOS 7 的应用程序.我正在尝试将 UISearchDisplayController 设置到导航栏中.

I am building an iOS 7-only app. I am trying to set a UISearchDisplayController into the navigation bar.

我的设置如下:在情节提要中,我在视图控制器的视图中添加了一个搜索栏和搜索显示控制器",并将其设置为相对于顶部布局指南的 (0,0).我将约束设置为固定到左侧、顶部和右侧.(我玩过约束,我完全删除了它们,没关系)最重要的是我有我的表格视图.当我将搜索栏添加到故事板中的视图时,它会自动为 searchDisplayController 和 searchBar 委托设置插座.在代码中我有 self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 我有两个问题:

I have it set up like this: In the storyboard, I added a "Search Bar and Search Display Controller" to my view controller's view, and set it at (0,0) relative to the top layout guide. I set constraints to pin to left, top and right. (I played with the constraints, i removed them completely, it doesn't matter) On top of that I have my Table view. When I added the search bar to the view in the storyboard, it automatically setup outlets for searchDisplayController and searchBar delegate. In code I have self.searchDisplayController.displaysSearchBarInNavigationBar = YES; I have two problems:

1) 没有为搜索栏显示任何按钮(界面构建器 -> 选择搜索栏 -> 选项:未选择),搜索栏位于屏幕中间:

1) Without any buttons showing for the search bar (Interface builder -> select search bar -> Options: none selected) the search bar is in the middle of the screen:

如果我点击导航栏,它会开始编辑搜索栏:

If I click on the navigation bar, it starts editing the search bar:

另请注意,深色覆盖层似乎与导航栏有偏移.在我看来,空间与导航栏的高度相同.就像它已经向下移动了那么多.另外,当它显示搜索结果时,内容视图的顶部向下移动了相同的量(后面有更多图片),这就引出了第二个问题.

notice also that the dark overlay appears to be offset from the navigation bar. It seems to me that the space is the same height as the navigation bar. Like it has been shifted down by that much. Also, when it displays the search results, the top of the content view is shifted down by the same amount (more pictures follow), which brings me to the second problem.

2) 我把它弄乱了一段时间,并决定选中让它显示取消按钮的选项.现在我将搜索栏正确嵌入到导航栏中,但叠加层仍然向下移动:

2) I messed around with it for a while and decided to check the option to have it show the cancel button. Now I have the search bar embedded in the nav bar correctly, but the overlay is still shifted down:

再次,当搜索结果表格视图出现时,它向下移动了相同的量(注意右侧的滚动条):

Again, when the search results table view appears, it is shifted down by the same amount (notice the scroll bar on the right side):

更奇怪的是,我在搜索显示控制器的 tableview 层上设置了一个边框,它看起来是正确的:

Even more bizarrely, I set a border on the search display controller's tableview layer, and it appears correct:

我以前从未使用过 UISearchDisplayController,也不熟悉如何设置它,但在功能上它运行良好.我已经阅读了一些其他类似的帖子,但唯一的建议是通过调整框架和设置手动偏移来解决它.我更想知道是什么原因造成的,这是一个错误吗?我做错了什么?如果这是一个错误,我可以等待修复.这似乎是一千人必须毫无问题地完成的基本事情,所以我觉得我没有正确设置它.感谢您的输入.

I have never used the UISearchDisplayController before and I unfamiliar with how to set it up, but functionally it works fine. I have read some other similar posts but the only advice is to hack it up by adjusting frames and setting manual offsets. I'd prefer to know what is causing this, is it a bug? Something I'm doing wrong? If it's a bug I can wait for a fix. It seems like such a basic thing that a thousand people must have done without any problem so I feel like I'm not setting it up correctly somehow. Thanks for you input.

推荐答案

我记得遇到了与您所观察到的完全相同的问题.您可以尝试几种解决方案.

I remember running into the same exact problem that you are observing.There could be a couple of solutions you can try.

  • 如果您使用故事板您应该单击为您的 tableview 设置的视图控制器或 TableView 控制器,然后转到其属性检查器并查看 ViewController 部分下,并将 Extend Edges 部分设置为 Top Bars 下.

  • If you are using storyboardsYou should click on the view controller or TableView Controller which you have set up for your tableview and go to its attribute inspector and look under ViewController section and set the Extend Edges section to be under Top Bars.

如果您不使用故事板,您可以使用 viewcontrollers edgeForExtendedLayout 属性手动设置设置,这应该可以解决问题.我使用的是故事板.

If you are not using storyboards you can manually set the settings using the viewcontrollers edgesForExtendedLayout property and that should do the trick. I was using storyboards.

这篇关于嵌入导航栏中时,iOS 7 中奇怪的 UISearchDisplayController 视图偏移行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:20