当 UISearchController 处于活动状态时,iOS 9 searchBar 从表头视图中消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32687240/
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
iOS 9 searchBar disappears from table header view when UISearchController is active
提问by David Trang
The structure:
结构:
View1 (click a button) -> present modally (MyModalView: UITableViewController)
View1(单击按钮)-> 模态呈现(MyModalView:UITableViewController)
MyModalView has UISearchController embedded. The searchBar of UISearchController is placed in MyModalView.tableView.tableHeaderView.
MyModalView 嵌入了 UISearchController。UISearchController 的 searchBar 放在 MyModalView.tableView.tableHeaderView 中。
It's been working fine since iOS 8.0. However on iOS 9, the searchBar disappear when the UISearchController is active. Please take a look at theses pictures below
自 iOS 8.0 以来一直运行良好。但是在 iOS 9 上,当 UISearchController 处于活动状态时,searchBar 会消失。请看下面的这些图片
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 测试版中...
Any idea / pointer would be deeply appreciated
任何想法/指针将不胜感激
Cheers.
干杯。
回答by Shwethascar
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. 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.
我的猜测是,UISearchController
当它试图作为导航栏呈现时,它正在做一些时髦的事情。所以,这是一个黑客,但它至少不会阻止用户。搜索栏没有做很酷的动画并掩盖了导航栏。
回答by David Trang
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.
我得到了解决我的问题的解决方案。它使用界面生成器在情节提要中将 Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) 设置为 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
延伸边缘: - 在顶部条下打勾 - 在底部条下打勾 - 在不透明条下打勾
回答by wiles duan
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;
}
回答by jigzat
I had to
我不得不
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
I found a similar question herebut in my case it was not on the viewDidLoad method. I had to try different views until it worked. Now I can have both a custom navigation bar color and the search bar,
我在这里发现了一个类似的问题,但就我而言,它不在 viewDidLoad 方法上。我不得不尝试不同的观点,直到它奏效。现在我可以同时拥有自定义导航栏颜色和搜索栏,
回答by patrick kuang
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 中实现以下 2 个方法
- (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;
}
回答by Phil Niedertscheider
I fixed it in my case by removing
我通过删除在我的情况下修复了它
definesPresentationContext = true
definesPresentationContext = true
I didn't test yet if there are any disadvantages of removing this!
我还没有测试删除它是否有任何缺点!
回答by Yassine ElBadaoui
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
.
我通过设置to 的definesPresentationContext
属性来修复它,并将其设置为包含.UISearchController
false
true
UITableViewController
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)
}
回答by igraczech
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];
}
回答by Victor Gomes
It works
有用
override func viewDidLoad() {
super.viewDidLoad()
self.extendedLayoutIncludesOpaqueBars = !self.navigationController!.navigationBar.translucent
}
回答by Per
Setting the navigationBar permanently to translucent in storyboard solved my problem.
在故事板中将导航栏永久设置为半透明解决了我的问题。