1
votes

I have implemented a search controller in a viewController with a table view and also a search controller, the search bar is displayed when search button is taped, search button is a UIBarButtonItem and the searchBar is shown in the navigation controller but I want to make disappear when tap on cancel button, I have tried using serarchBarDelegate protocol but nothing happens on tap here the code

    class NewsTVController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate{
let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        definesPresentationContext = true
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = true

        self.searchController.hidesNavigationBarDuringPresentation = false

        loadNews()
    }


@IBAction func searchButtonTaped(sender:UIBarButtonItem){
        print("tap inside")
        self.navigationItem.titleView = searchController.searchBar
        //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
       // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

        //}
        //self.navigationItem.rightBarButtonItem.
    }
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        hideSearchBar()
    }
    func hideSearchBar() {
        print("hay que escpder")
        //navigationItem.setLeftBarButtonItem(searchBarButtonItem, animated: true)
        //logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = nil// = self.logoImageView
            self.navigationItem.title = "Eventos"
            //self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }  
}

here the pic of the navigation controller with a search button and the search bar enter image description here

what I want is that when the cancel button is tapped the navigation bar stay as in first pic, no search bar. and some tips there is a way to hide the back button and the search button when search bar appears.

1

1 Answers

0
votes

as i donĀ“t find a way to achive this i found this solution

@IBAction func searchButtonTaped(sender:UIBarButtonItem){
        print("tap inside")
        self.navigationItem.titleView = searchController.searchBar
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:)))
        //self.searchIcon.is
        //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
       // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

        //}
        //self.navigationItem.rightBarButtonItem.
    }
func hideSearchBar(sender:UIBarButtonItem) {
        print("hay que escpder")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:)))
        //logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = nil// = self.logoImageView

            self.navigationItem.title = "Noticias"
            //self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }

and boila the search bar now dissapear from navigation controller nad the search button is functional since is linked to searchButtonTaped in Interface Builder