iOS 9 当 UISearchController 处于活动状态时,searchBar 从 table header 视图中消失
iOS 9 searchBar disappears from table header view when UISearchController is active
结构:
View1(点击一个按钮)-> 模态呈现(MyModalView:UITableViewController)
MyModalView 嵌入了 UISearchController。 UISearchController的searchBar放在MyModalView.tableView.tableHeaderView.
自 iOS 8.0 以来一直运行良好。但是在 iOS 9 上,当 UISearchController 处于活动状态时,searchBar 会消失。请看下面这些图片
模态视图:
UISearchController 在 iOS 8:
上激活
UISearchController 在 iOS 9:
上激活
非常标准的代码:
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)
}
此问题也发生在 iOS 9.1 beta 中...
任何想法/指示将不胜感激
干杯。
我通过删除
修复了我的情况
definesPresentationContext = true
我还没有测试删除它是否有任何缺点!
我不确定到底是什么问题,但我 'fixed' 通过:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
我的猜测是 UISearchController
在尝试显示为导航栏时做了一些奇怪的事情。所以,这是一个 hack,但它至少不会阻止用户。搜索栏没有做很酷的动画并掩盖了导航栏。
我发现是情节提要中的模拟指标(顶部栏)导致了这个问题。
就我而言,以下几行有效,但我仍然不知道为什么。
- (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;
}
在情节提要中将导航栏永久设置为半透明解决了我的问题。
看来我们所有人都遇到了同样的问题,但解决方法不同。但是 none 的建议答案对我有用 :(。不过还是谢谢大家的宝贵时间。
我找到了解决我问题的方法。它使用 Interface Builder 在 Storyboard 中将 Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) 设置为 true。
总结:
MyModalView: UITableViewController,在 Storyboard using Interface Builder 中有
延伸边缘:
- 在顶栏下勾选
- 在底栏下勾选
- 在不透明条下打勾
我不得不
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
我发现了一个类似的问题 here 但在我的例子中它不在 viewDidLoad 方法上。我不得不尝试不同的观点,直到它奏效。现在我可以同时拥有自定义导航栏颜色和搜索栏了,
感谢@wiles duan 和@Techprimate
就我而言,我通过设置解决了这个问题:
self.definesPresentationContext = NO;
并在 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;
}
如果您想隐藏导航栏,并全屏显示搜索控制器,请在您的导航栏上设置以下内容,搜索栏不会消失:
navigationController?.navigationBar.translucent = true
我的应用这个地方没有导航栏。 None 的其他 SO 帖子对我有帮助,所以我用这种方式修复了它:
- (void)layoutSubviews
{
[[[self searchController] searchBar] sizeToFit];
}
有效
override func viewDidLoad() {
super.viewDidLoad()
self.extendedLayoutIncludesOpaqueBars = !self.navigationController!.navigationBar.translucent
}
我有同样的问题,当我在Xcode上调试UI时,我发现UISearchBar
视图被移动到另一个视图并且宽度被归零。
我通过将 UISearchController
的 definesPresentationContext
属性 设置为 false
并将包含的 UITableViewController
设置为 true
来修复它.
我只在你的 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)
}
sc.hidesNavigationBarDuringPresentation = false
对我有用
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 其中对我有用,我用这个 hack
修复了它
func position(for bar: UIBarPositioning) -> UIBarPosition {
if UIDevice.current.userInterfaceIdiom == .pad {
return .top
} else {
if iOSVersion <= 9 {
return .top
}
return .topAttached
}
}
结构:
View1(点击一个按钮)-> 模态呈现(MyModalView:UITableViewController)
MyModalView 嵌入了 UISearchController。 UISearchController的searchBar放在MyModalView.tableView.tableHeaderView.
自 iOS 8.0 以来一直运行良好。但是在 iOS 9 上,当 UISearchController 处于活动状态时,searchBar 会消失。请看下面这些图片
模态视图:
UISearchController 在 iOS 8:
UISearchController 在 iOS 9:
非常标准的代码:
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)
}
此问题也发生在 iOS 9.1 beta 中...
任何想法/指示将不胜感激
干杯。
我通过删除
修复了我的情况definesPresentationContext = true
我还没有测试删除它是否有任何缺点!
我不确定到底是什么问题,但我 'fixed' 通过:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
我的猜测是 UISearchController
在尝试显示为导航栏时做了一些奇怪的事情。所以,这是一个 hack,但它至少不会阻止用户。搜索栏没有做很酷的动画并掩盖了导航栏。
我发现是情节提要中的模拟指标(顶部栏)导致了这个问题。 就我而言,以下几行有效,但我仍然不知道为什么。
- (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;
}
在情节提要中将导航栏永久设置为半透明解决了我的问题。
看来我们所有人都遇到了同样的问题,但解决方法不同。但是 none 的建议答案对我有用 :(。不过还是谢谢大家的宝贵时间。
我找到了解决我问题的方法。它使用 Interface Builder 在 Storyboard 中将 Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) 设置为 true。
总结:
MyModalView: UITableViewController,在 Storyboard using Interface Builder 中有
延伸边缘: - 在顶栏下勾选 - 在底栏下勾选 - 在不透明条下打勾
我不得不
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
我发现了一个类似的问题 here 但在我的例子中它不在 viewDidLoad 方法上。我不得不尝试不同的观点,直到它奏效。现在我可以同时拥有自定义导航栏颜色和搜索栏了,
感谢@wiles duan 和@Techprimate
就我而言,我通过设置解决了这个问题:
self.definesPresentationContext = NO;
并在 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;
}
如果您想隐藏导航栏,并全屏显示搜索控制器,请在您的导航栏上设置以下内容,搜索栏不会消失:
navigationController?.navigationBar.translucent = true
我的应用这个地方没有导航栏。 None 的其他 SO 帖子对我有帮助,所以我用这种方式修复了它:
- (void)layoutSubviews
{
[[[self searchController] searchBar] sizeToFit];
}
有效
override func viewDidLoad() {
super.viewDidLoad()
self.extendedLayoutIncludesOpaqueBars = !self.navigationController!.navigationBar.translucent
}
我有同样的问题,当我在Xcode上调试UI时,我发现UISearchBar
视图被移动到另一个视图并且宽度被归零。
我通过将 UISearchController
的 definesPresentationContext
属性 设置为 false
并将包含的 UITableViewController
设置为 true
来修复它.
我只在你的 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)
}
sc.hidesNavigationBarDuringPresentation = false
对我有用
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 其中对我有用,我用这个 hack
修复了它func position(for bar: UIBarPositioning) -> UIBarPosition {
if UIDevice.current.userInterfaceIdiom == .pad {
return .top
} else {
if iOSVersion <= 9 {
return .top
}
return .topAttached
}
}