[英]UISearchController's UISearchBar shifts incorrectly and jumps when UITableView is scrolled
In the video, it's a little difficult to see what's going on, so I'll try to explain it.在视频中,很难看到发生了什么,所以我会尝试解释它。 https://youtu.be/yOrCJB9yZlg
https://youtu.be/yOrCJB9yZlg
I have UIViewController
with UITableView
inside its root view.我在其根视图中有
UIViewController
和UITableView
。 Binded via Autolayout
(there is no difference how it's binded: to SuperView or to SafeArea).通过自动
Autolayout
(绑定方式没有区别:到 SuperView 或到 SafeArea)。
SearchBar added like this: let search = UISearchController(searchResultsController: nil) self.navigationItem.searchController = search
SearchBar 添加如下:
let search = UISearchController(searchResultsController: nil) self.navigationItem.searchController = search
When I slowly scroll table, the transition between large navbar and compact navbar, and then animation of showing in and out of searchBar is too fast.当我慢慢滚动表格时,大导航栏和紧凑导航栏之间的过渡,然后显示进出searchBar的动画太快了。 Searchbar jumping in/out of navbar instead of smoothly opened/closed;
搜索栏跳入/跳出导航栏,而不是顺利打开/关闭; navbar transitioning is jumping between two states, large and compact, without smoothly passes through the middle half-opened state while you slowly moving your finger on screen, like in system apps (Mail, Phone, Messages, Contacts etc).
导航栏过渡在两种状态之间跳跃,大而紧凑,当您在屏幕上缓慢移动手指时,不会顺利通过中间的半开状态,就像在系统应用程序(邮件、电话、消息、联系人等)中一样。
I made the example from an empty project to demonstrate the issue;我从一个空项目中制作了这个例子来演示这个问题; there isn't any changes to navigation bar logic, or any logic at all.
导航栏逻辑没有任何变化,或者根本没有任何逻辑。 Just two new VC's and this odd behaviour.
只是两个新的 VC 和这种奇怪的行为。
If i create xCode's "Master-Details" project example and add UISearchController
to it, it will work properly.如果我创建 xCode 的“Master-Details”项目示例并将
UISearchController
添加到其中,它将正常工作。 I assume its because they used UITableViewController
instead of UIViewController
+ UITableView
inside.我认为这是因为他们在里面使用了
UITableViewController
而不是UIViewController
+ UITableView
。
What the reason of this behaviour and how to fix it?这种行为的原因是什么以及如何解决?
What you have done is correct.你所做的是正确的。 Did you try running your code in a device?
您是否尝试在设备中运行您的代码? Feels more like a glitch in the simulator.
感觉更像是模拟器中的小故障。 I tried what you tried and it works fine for me in the device.
我尝试了您尝试的方法,并且在设备中对我来说效果很好。 Whereas the glitch occurs in simulator.
而故障发生在模拟器中。
Refer to this article .参考这篇文章。 They have explained step by step process.
他们已经解释了一步一步的过程。
This is a known problem and your code seems fine.这是一个已知问题,您的代码看起来不错。 This problem was already discussed here .
这个问题已经在这里讨论过。
Problem appears when you're using UIScrollView
or its subclasses with large navigation titles.当您使用带有大导航标题的
UIScrollView
或其子类时会出现问题。 It doesn't work.它不起作用。 Problem disappears when you use
UICollectionViewController
or UITableViewController
instead.当您改用
UICollectionViewController
或UITableViewController
时,问题就会消失。
I had the same problem with the search controller transition and tableview.我在搜索控制器转换和 tableview 上遇到了同样的问题。 like this its flickering or tableview was jumping.
像这样它的闪烁或 tableview 正在跳跃。 The search controller was on the navigation item.
搜索控制器位于导航项上。
The key solution is to remove safe area guide from your view controller and assign top bottom left right constraints to your tableview.关键的解决方案是从您的视图控制器中删除安全区域指南,并为您的 tableview 分配左上右下约束。 It will be smooth like this
会像这样顺利
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.