ios 由 UISearchController 在表头视图中呈现的 UISearchBar 在活动时动画太远

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28326269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:40:57  来源:igfitidea点击:

UISearchBar presented by UISearchController in table header view animates too far when active

iosuitableviewios8uisearchbaruisearchcontroller

提问by Zo? Smith

I am using UISearchController to present a search bar inside the header view of a tableview:

我正在使用 UISearchController 在 tableview 的标题视图中显示一个搜索栏:

...
self.searchController.hidesNavigationBarDuringPresentation = NO;            
self.presentingTVC.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.presentingTVC.tableView.tableHeaderView = self.searchController.searchBar;

(where setting the tableHeaderView property twice is necessary as otherwise the header view overlaps the first row, c.f.a couple of answers on S.O.)

(其中两次设置 tableHeaderView 属性是必要的,否则标题视图与第一行重叠,参见SO 上的几个答案

This is how it looks, perfectly in position when inactive: Inactive search bar in correct position

这是它的外观,在不活动时完美定位: 处于正确位置的非活动搜索栏

The search bar should just stay in place when active - I don't want it to move up to hide the navigation bar. But it unexpectedly animates down, leaving a blank space between it and the navigation bar:

搜索栏应该在活动时保持原位 - 我不希望它向上移动以隐藏导航栏。但它意外地动画下来,在它和导航栏之间留下了一个空白:

Screenshot showing search bar animated too far down

屏幕截图显示搜索栏动画太远

Here's a video of weird search bar animation

这是一段 奇怪的搜索栏动画视频

If I just use a search bar separately from UISearchController, it does not show the same behaviour when it becomes active.

如果我只使用与 UISearchController 分开的搜索栏,它在变为活动状态时不会显示相同的行为。

In my presenting view controller, I have self.definesPresentationContext = YES;and self.navigationController.navigationBar.translucent = YES;, and in IB none of the "extend edges" boxes are active (all seemed to be possible things that could throw search presentation off, from reading around).

在我的呈现视图控制器中,我有self.definesPresentationContext = YES;self.navigationController.navigationBar.translucent = YES;,并且在 IB 中没有一个“扩展边缘”框处于活动状态(所有似乎都是可能的事情,可能会导致搜索演示从阅读中消失)。

Does anyone know how I can stop the search bar from animating down?

有谁知道如何阻止搜索栏向下动画?

采纳答案by Brian Sachetta

Ok so I finally figured out a solution for myself. Though it's more than likely we have other things in our code / storyboard (that's why this is a hard question to answer), for the most part I followed Apple's tutorial on UISearchController: (https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html)

好的,所以我终于为自己找到了解决方案。尽管我们的代码/故事板中很可能还有其他内容(这就是为什么这是一个很难回答的问题),但在大多数情况下,我遵循了 Apple 关于 UISearchController 的教程:(https://developer.apple.com/library/ ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html)

Just about all my code is identical to theirs, but I couldn't get the search bar to not jump when clicking inside of it. So what I did was check "under opaque bars" for both the original table view and the search results table view in my storyboard. That got the search bar to stop jumping.

几乎我所有的代码都与他们的代码相同,但是在搜索栏内部单击时,我无法让搜索栏不跳转。所以我所做的是在我的故事板中检查原始表格视图和搜索结果表格视图的“在不透明条下”。这让搜索栏停止跳跃。

But there was one last issue, which was the fact that the first row of the results table view was hidden by the search bar. In order to fix that I added self.tableView.contentInset = UIEdgeInsetsMake(kHeightOfTableViewCells, 0, 0, 0);to the viewDidLoadmethod of my results table view. Voila!

但还有最后一个问题,即结果表视图的第一行被搜索栏隐藏了。为了解决这个问题,我添加self.tableView.contentInset = UIEdgeInsetsMake(kHeightOfTableViewCells, 0, 0, 0);viewDidLoad我的结果表视图的方法中。瞧!

回答by ppilone

Old question but I was able to solve this issue by setting

老问题,但我能够通过设置解决这个问题

self.extendedLayoutIncludesOpaqueBars = YES;

self.extendedLayoutIncludesOpaqueBars = YES;

on my view controller. I think is issue is caused by having an opaque navigation bar but setting hidesNavigationBarDuringPresentationto NOon your search controller, causing the search bar to incorrectly position itself when focused.

在我的视图控制器上。我认为问题是由于导航栏不透明,但在搜索控制器上设置hidesNavigationBarDuringPresentationNO,导致搜索栏在聚焦时定位不正确。

回答by Piotr Tomasik

I was able to prevent UISearchBarfrom shifting down by removing the following line from my ViewController: self.definesPresentationContext = YES;

UISearchBar通过从我的 ViewController 中删除以下行,我能够防止向下移动: self.definesPresentationContext = YES;

回答by Gleb

definesPresentationContext = true

定义PresentationContext = true

override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false

        searchController.dimsBackgroundDuringPresentation = true
        searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
        self.tableView.tableHeaderView = searchController.searchBar

        definesPresentationContext = true

回答by Sozin's Comet

My turn. I also had this problem when I followed the WWDC sample code.

轮到我了。我在遵循 WWDC 示例代码时也遇到了这个问题。

I noticed that if I didn't set the scopeButtonTitles property of the searchBar, the searchBar would not be visible. Upon closer inspection, it just had a frame of CGRectZero. This means that setting the scopeButtonTitles sets the frame behind the scenes. So If don't want to show any scopeButtonTitles, but still want to not have to hardcode UISearchBar to a specific height, set the scopeButtonTitles to an empty array.

我注意到如果我没有设置 searchBar 的 scopeButtonTitles 属性,则 searchBar 将不可见。仔细检查后,它只有一个 CGRectZero 框架。这意味着设置 scopeButtonTitles 会在幕后设置框架。因此,如果不想显示任何 scopeButtonTitles,但仍不想将 UISearchBar 硬编码到特定高度,请将 scopeButtonTitles 设置为空数组。

self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.scopeButtonTitles = []
self.tableView.tableHeaderView = self.searchController.searchBar

Setting the scopeButtonTitles to an array of 1 strings will not display the scope button titles, but still have logic to deal with the view, essentially screwing up the layout.

将 scopeButtonTitles 设置为 1 个字符串的数组将不会显示范围按钮标题,但仍然具有处理视图的逻辑,基本上会搞砸布局。

Props to Apple's QA team (applicable to iOS 8)

给 Apple QA 团队的道具(适用于 iOS 8)

回答by M. Porooshani

I ran to this problem just recently and none of the solutions worked for me. Maybe because my UI is more complex (My table view with search supports in contained in UITabController inside a UiNavigationController) anyway, none of the above worker. I could simply solve my problem by this very neat piece of code I found at: UISearchController Not Redisplaying Navigation Bar on Rotate

我最近遇到了这个问题,但没有一个解决方案对我有用。可能是因为我的 UI 更复杂(我的带有搜索支持的表视图包含在 UiNavigationController 内的 UITabController 中),无论如何,上述工作人员都没有。我可以通过我在以下位置找到的这段非常简洁的代码来简单地解决我的问题: UISearchController Not Redisplaying Navigation Bar on Rotate

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    if (bar == searchController.searchBar) {
        return UIBarPositionTopAttached;
    }
    else { // Handle other cases
        return UIBarPositionAny;
    }
}

One more thing to add:after setting all my controller's hierarchy to extend under opaque and top bars, my search bar started to appear hidden under navigation bar and would've appeared right just after a rotation might happened. that was a cue for me that there is some lay out error and some information abut my layout was not right. this delegate method helps my controller understand the situation about the otherwise nomad search bar!

要添加的另一件事:将我的所有控制器的层次结构设置为在不透明和顶部栏下扩展后,我的搜索栏开始隐藏在导航栏下方,并且会在可能发生旋转后立即出现。这对我来说是一个提示,有一些布局错误和一些信息与我的布局不正确。此委托方法可帮助我的控制器了解有关其他游牧搜索栏的情况!

回答by Zo? Smith

Found it! This seems to be the offensive line in viewDidLoadof the presenting vc:

找到了!这似乎是viewDidLoad演示 vc的进攻线:

self.edgesForExtendedLayout = UIRectEdgeNone;

Removed that and the search bar stayed in place as predicted.

删除它,搜索栏按预期留在原地。

回答by Basil Mariano

unchecking Adjust Scroll View Insets

取消选中调整滚动视图插入

and

checking Under Top Bars

检查顶栏下方

Fixedmy problementer image description here

修复了我的问题在此处输入图片说明

回答by side

for iOS 11 & swift 4, set the line bellow in the viewController resolved my issue (searchBar jump down):

对于 iOS 11 和 swift 4,在 viewController 中设置下面的行解决了我的问题(searchBar jump down):

self.edgesForExtendedLayout = .bottom

回答by Hawe

I guess you set UISearchBar frame.original.y = 64

我猜你设置了 UISearchBar frame.original.y = 64

there are the code

有代码

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
}

searchBar.frame = CGRectMake(0, 0, 376, 44);

wash can help you

洗可以帮到你