vendredi 24 juin 2016

SearchController issues in Navigation Bar


I'm trying to create a search bar that appears on top of the nav bar when you click on "Search" (which is also in the nav bar). Clicking on a search result will take you to a Detail page where the Search Bar will disappear. When you click to go back to the Home VC, the search bar reappears. Thought it would be simple but encountering a few issues. This is what I've done so far To create the search bar when you tap "Search" in the HomeVC I did this: @IBAction func SearchAction(sender: AnyObject) { searchController.searchResultsUpdater = self searchController.searchBar.delegate = self definesPresentationContext = true searchController.dimsBackgroundDuringPresentation = false searchController.hidesNavigationBarDuringPresentation = false self.navigationController?.presentViewController(searchController, animated: true, completion: nil) } 1st issue, when you go to the Detail page, it search bar doesn't disappear. Solution: In PrepareForSegue, I added self.searchController.searchBar.removeFromSuperview() and replaced the unwindSegue with a delegate so that when the user goes back to Home VC from Detail page, the search bar is added back in. The HomeVC performs the following: func didDismissDetail() { self.navigationController?.popViewControllerAnimated(true) if searchController.active == true { self.navigationController?.view.addSubview(searchController.searchBar) } } 2nd issue: There's a bug in the scenario where you search, tap to go to the Detail page, go back to Home VC, tap Cancel to close the Search controller. When you click on Search button in the nav bar again, nothing happens. I get this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .' Solution: I added this func searchBarCancelButtonClicked(searchBar: UISearchBar) { self.searchController.searchBar.removeFromSuperview() } This works however, there is no animation when the view is removed. Sorry, I know it's a little long winded - my question is: Is there a better way to create this user experience? If not, how can you animate the removal of the search bar when the user taps on Cancel Here are some images:

Aucun commentaire:

Enregistrer un commentaire