我试图将SearchController添加到UINavigationBar。我试图在UINavigationController的后退按钮之后设置UISearchBar的UITextField。我想在后退按钮后有更多空间
当我开始搜索时,它显示为
然而,我应该能够查看后退按钮。只应减少文本字段宽度。在取消之后,它应该再次回到初始布局。而它显示如下:
以下是我的代码
var searchResultController = UISearchController()
self.searchResultController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
controller.hidesNavigationBarDuringPresentation = false
self.navigationController?.navigationBar.addSubview(controller.searchBar)
return controller
})()
override func viewDidAppear(animated: Bool) {
for subView in searchResultController.searchBar.subviews{
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.width = self.view.bounds.width - 50
}
}
}
}
请告诉我如何解决此问题。
提前致谢
答案 0 :(得分:1)
要设置UIsearchBar,请将其添加到导航的titleView为
self.navigationItem.titleView = controller.searchBar
要删除取消按钮,我们可以使用UISearchControllerDelegate方法
func didPresentSearchController(searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
希望这对任何人都有帮助。