Ios 向导航添加完整的搜索栏
我正在向导航栏添加一个搜索栏,如下所示Ios 向导航添加完整的搜索栏,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,我正在向导航栏添加一个搜索栏,如下所示 searchBar.showsCancelButton = false searchBar.placeholder = "Search" searchBar.delegate = self searchBar.enablesReturnKeyAutomatically = true self.navigationItem.titleView = searchBar 但是我有一个工具栏按钮项目,有没有办法隐藏它,并使
searchBar.showsCancelButton = false
searchBar.placeholder = "Search"
searchBar.delegate = self
searchBar.enablesReturnKeyAutomatically = true
self.navigationItem.titleView = searchBar
但是我有一个工具栏按钮项目,有没有办法隐藏它,并使搜索栏保持完整的长度呢?试试下面的代码: 注意:当搜索栏处于活动状态时,使用按钮隐藏功能更新代码;当按下取消按钮时,代码返回
class ViewController: UIViewController,UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var resultSearchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.delegate = self
self.resultSearchController.searchBar.delegate = self
self.resultSearchController.hidesNavigationBarDuringPresentation = false
self.resultSearchController.dimsBackgroundDuringPresentation = true
self.definesPresentationContext = true
self.navigationItem.titleView = resultSearchController.searchBar
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Your Button", style: .plain, target: self, action: #selector(addTapped))
}
func updateSearchResults(for searchController: UISearchController) {
// You have to implement search delegate method here to make it work.
if resultSearchController.isActive == true {
self.navigationItem.rightBarButtonItem = nil
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Your Button", style: .plain, target: self, action: #selector(addTapped))
}
}
func addTapped() {
// You can do your stuff here. when your button pressed...
print("Button Pressed")
}
}
输出:
你有没有查看我的更新……啦啦队老师,我无法提升我的声誉,但谢谢你