[英]iOS 9 searchBar disappears from table header view when UISearchController is active
The structure: 结构:
View1 (click a button) -> present modally (MyModalView: UITableViewController) View1(单击按钮) - >以模态方式呈现(MyModalView:UITableViewController)
MyModalView has UISearchController embedded. MyModalView嵌入了UISearchController。 The searchBar of UISearchController is placed in MyModalView.tableView.tableHeaderView.
UISearchController的searchBar放在MyModalView.tableView.tableHeaderView中。
It's been working fine since iOS 8.0. 自iOS 8.0起,它一直运行良好。 However on iOS 9, the searchBar disappear when the UISearchController is active.
但是在iOS 9上,当UISearchController处于活动状态时,searchBar会消失。 Please take a look at theses pictures below
请看下面这些图片
UISearchController active on iOS 8: UISearchController在iOS 8上处于活动状态:
UISearchController active on iOS 9: UISearchController在iOS 9上处于活动状态:
The very standard code: 非常标准的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Dynamically create a search controller using anonymous function
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Auto sizing row & cell height
self.tableView.estimatedRowHeight = 130
self.tableView.rowHeight = UITableViewAutomaticDimension
self.definesPresentationContext = true
// No footer for better presentation
self.tableView.tableFooterView = UIView.init(frame: CGRectZero)
}
This issue also happens in iOS 9.1 beta... 这个问题也发生在iOS 9.1 beta ...
Any idea / pointer would be deeply appreciated 任何想法/指针都将深受赞赏
Cheers. 干杯。
I'm not sure what exactly is the problem but I 'fixed' it by: 我不确定究竟是什么问题,但我通过以下方式解决了这个问题:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
My guess is that UISearchController
is doing something funky when it is trying to present as a navigation bar. 我的猜测是,
UISearchController
在尝试呈现为导航栏时正在做一些时髦的事情。 So, this is a hack but it at least doesn't block the user. 所以,这是一个黑客,但它至少不会阻止用户。 The search bar doesn't do the cool animation and cover up the navigation bar.
搜索栏不会执行很酷的动画并掩盖导航栏。
It seems all of us got the same problem but they were solved in different ways. 似乎我们所有人都遇到了同样的问题,但他们以不同的方式解决了。 However none of the suggested answers worked for me :(. Nevertheless thank you all for your time.
然而,没有一个建议的答案对我有用:(。尽管如此,谢谢大家的时间。
I got a solution that solved my problem. 我找到了解决问题的解决方案。 It is setting Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) to true in the Storyboard using Interface Builder.
它使用Interface Builder在Storyboard中将我的(MyModalView:UITableViewController)的Opaque Bars下的Extend Edges设置为true。
In summary: 综上所述:
MyModalView: UITableViewController, in Storyboard using Interface Builder has MyModalView:UITableViewController,在Storyboard中使用Interface Builder
Extend Edges: - Under Top Bars ticked - Under Bottom Bars ticked - Under Opaque Bars ticked 延伸边缘: - 在顶栏下方勾选 - 在底栏下方勾选 - 在不透明栏下勾选
I found it's the simulated metrics (top bar) in storyboard that's cause this problem. 我发现故事板中的模拟指标(顶部栏)导致了这个问题。 In my case, the following lines work, but I still don't know why.
在我的情况下,以下几行有效,但我仍然不知道为什么。
- (void)willPresentSearchController:(UISearchController *)searchController {
// do something before the search controller is presented
self.navigationController.navigationBar.translucent = YES;
}
-(void)willDismissSearchController:(UISearchController *)searchController
{
self.navigationController.navigationBar.translucent = NO;
}
I had to 我不得不
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
I found a similar question here but in my case it was not on the viewDidLoad method. 我在这里找到了一个类似的问题,但在我的情况下,它不在viewDidLoad方法上。 I had to try different views until it worked.
我必须尝试不同的观点,直到它奏效。 Now I can have both a custom navigation bar color and the search bar,
现在我可以同时拥有自定义导航栏颜色和搜索栏,
Thanks @wiles duan and @Techprimate 谢谢@wiles duan和@Techprimate
In my case, I fixed this issue by setting: 就我而言,我通过设置修复了这个问题:
self.definesPresentationContext = NO;
And implement the following 2 methods in UISearchControllerDelegate 并在UISearchControllerDelegate中实现以下两个方法
- (void)willPresentSearchController:(UISearchController *)searchController {
// do something before the search controller is presented
self.navigationController.navigationBar.translucent = YES;
}
-(void)willDismissSearchController:(UISearchController *)searchController
{
self.navigationController.navigationBar.translucent = NO;
}
I fixed it in my case by removing 我通过删除修复了它
definesPresentationContext = true
I didn't test yet if there are any disadvantages of removing this! 如果删除这个有任何缺点,我还没有测试!
I don't have a navigation bar in this place of an app. 我在这个应用程序的位置没有导航栏。 None of other SO posts helped me, so I've fixed it this way:
没有其他SO帖子对我有帮助,所以我用这种方式解决了这个问题:
- (void)layoutSubviews
{
[[[self searchController] searchBar] sizeToFit];
}
I had the same problem, and when I debugged the UI on Xcode I found that the UISearchBar
view was moved to another view and the width was zeroed. 我有同样的问题,当我在Xcode上调试UI时,我发现
UISearchBar
视图被移动到另一个视图并且宽度为零。
I fixed it by setting definesPresentationContext
property of the UISearchController
to false
, and setting it true
for the containing UITableViewController
. 我通过将
UISearchController
definesPresentationContext
属性设置为false
来修复它,并将其设置为包含UITableViewController
true
。
I added only one line to your viewDidLoad()
. 我只在
viewDidLoad()
添加了一行。
override func viewDidLoad() {
super.viewDidLoad()
// Dynamically create a search controller using anonymous function
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.definesPresentationContext = false // Disable the presentation controller
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Auto sizing row & cell height
self.tableView.estimatedRowHeight = 130
self.tableView.rowHeight = UITableViewAutomaticDimension
self.definesPresentationContext = true // This one remains the same
// No footer for better presentation
self.tableView.tableFooterView = UIView.init(frame: CGRectZero)
}
在故事板中将navigationBar永久设置为半透明解决了我的问题。
It works 有用
override func viewDidLoad() {
super.viewDidLoad()
self.extendedLayoutIncludesOpaqueBars = !self.navigationController!.navigationBar.translucent
}
如果要隐藏导航栏,并在全屏显示搜索控制器,请在导航栏上设置以下内容,搜索栏不会消失:
navigationController?.navigationBar.translucent = true
sc.hidesNavigationBarDuringPresentation = false
does the trick for me 为我做的伎俩
lazy var searchController:UISearchController = {
let sc = UISearchController(searchResultsController: nil)
sc.searchResultsUpdater = self
sc.obscuresBackgroundDuringPresentation = false
sc.searchBar.placeholder = "Search"
sc.hidesNavigationBarDuringPresentation = false
return sc
}()
None of them worked for me, I fixed it using this hack 他们都没有为我工作,我用这个黑客修复了它
func position(for bar: UIBarPositioning) -> UIBarPosition {
if UIDevice.current.userInterfaceIdiom == .pad {
return .top
} else {
if iOSVersion <= 9 {
return .top
}
return .topAttached
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.