在 iOS 11 中,我们现在使用
self.navigationItem.searchController = self.searchController
在导航项标题 View 中嵌入搜索栏。然而,这会将其他导航项推到搜索栏上方,如下所示:
在不使用自定义容器或回到设置搜索栏的旧方法的情况下,我们有什么办法可以让其他导航项与 iOS 11 中的搜索栏处于同一级别?像这样:
最佳答案
您可以尝试通过像这样更改取消按钮来实现此目的:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.showsCancelButton = true
if let view = searchController.searchBar.subviews.first {
for subview in view.subviews {
if let myButton = subview as? UIButton {
// setup button
myButton.setTitle("", for: .normal)
let image = UIImage(named: "myImage")
myButton.setBackgroundImage(image, for: .normal)
}
}
}
navigationItem.searchController = searchController
关于iOS 11 : Add other nav items next to a search bar embedded in navigation item title view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47118361/