Je veux montrer un UISearchController dans UINavigationController, quand on clique sur le bouton droit de UINavigationController et le cacher quand on clique à nouveau sur le bouton.Comment faire pour afficher par programme UISearchController dans le leftbarbuttonitem UINavigationController et le cacher après la recherche?
Initially the UINavigationBar will be like this
Plus tard,
The UINavigationBar should look like this, when search is clicked
et plus lorsque vous cliquez dessus sur l'icône près, la UINavigationBar devrait ressembler à image1.
Le code que je utilise pour montrer SearchController est,
func setUpSearchBar(){
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchBar.showsCancelButton = true
var cancelButton: UIButton
let topView: UIView = self.searchController.searchBar.subviews[0] as UIView
for subView in topView.subviews {
if let pvtClass = NSClassFromString("UINavigationButton") {
if subView.isKind(of: pvtClass) {
cancelButton = subView as! UIButton
cancelButton.setTitle("", for: .normal)
cancelButton.tintColor = UIColor.blue
cancelButton.setImage(UIImage(named:"close"), for: .normal)
cancelButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
}
}
}
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name:"Futura-Medium", size:20)!,NSForegroundColorAttributeName:UIColor(red: 234/255, green: 234/255, blue: 234/255, alpha: 1.0)]
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.searchBar.tintColor = UIColor(red: 56/255, green: 192/255, blue: 201/255, alpha: 1.0)
self.searchController.searchBar.backgroundColor = UIColor.clear
self.searchController.searchBar.barStyle = .black
// self.navigationItem.titleView = searchController.searchBar
let leftNavBarButton = UIBarButtonItem(customView: self.searchController.searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
self.searchController.searchBar.becomeFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchController.searchBar.resignFirstResponder()
self.navigationItem.leftBarButtonItem?.isEnabled = false
self.navigationItem.leftBarButtonItem?.tintColor = .clear
}
func pressButton(button: UIButton) {
searchEvent.isEnabled = true
}
func updateSearchResults(for searchController: UISearchController) {
print(searchController.searchBar.text)
}
Mais ce code montre la barre de navigation, mais quand on clique sur le bouton annuler, l'événement presserBouton ne se déclenche? Pourquoi?
Retourné. Si vous avez une nouvelle question, créez un post de question distinct. Vous annulez la réponse, bonne ou mauvaise, en changeant la question. Vous pouvez décider d'accepter/de modifier la réponse. – niton