The navigation bar disappears in the search result table view - ios

I am trying to have a UISearchController in the navigation bar and display the results in an external controller.
For some reason the navigation bar disappear as soon as I type something in
I have been trying different solutions for a few hours with no results. It looks like it is a similar issue as Navigation bar disappears when typing in UISearchController text field and
Navigation bar disappears if reload data with UISearchController that did not get any answer.
self.cearchController = ({
//creating another tableview
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let alternateController:SearchResultsTableViewController = storyBoard.instantiateViewControllerWithIdentifier("SearchResultsTableViewController") as! SearchResultsTableViewController
let controller = UISearchController(searchResultsController: alternateController)
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchResultsUpdater = alternateController
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Search"
self.navigationItem.titleView = controller.searchBar
return controller
})()
I have tried
self.navigationController?.setNavigationBarHidden(true, animated: false)
and I have
myResultsTableView.definesPresentationContext = true
in the viewdidload
this is what it looks like :
Note: I have only started with swift a few days ago so I might be missing something really obvious!!
Thanks and happy to add more code

So I had a similar issue of the navbar disappearing when my search results were shown. But controller.hidesNavigationBarDuringPresentation = false did the trick for me.
Maybe try using tableView.tableHeaderView = searchController.searchBar instead of self.navigationItem.titleView = controller.searchBar

Related

UISearchBar disappears from NavigationItem after pushing a view controller

I have a UISearchBar embedded in the NavigationItem, like so:
let search = UISearchController(searchResultsController: nil)
search.searchBar.delegate = self
search.hidesNavigationBarDuringPresentation = false
search.definesPresentationContext = false
search.delegate = self
navigationItem.searchController = search
And I also have a collectionView.
When I push a viewController from the collectionview, and then go back, I can't see the UISearchBar anymore.
Any way of fixing this?

Does UISearchContainerViewController work on iOS

UISearchContainerViewController exists both on tvOS and on iOS.
Apple has sample code showing how to use it in tvOS: they configure a UISearchController, hand it to a UISearchContainerViewController, wrap that in a UINavigationController, and make it one of a UITabBarController's children.
But I have never seen an example of UISearchContainerViewController on iOS, and I can't make it work there. For example, I do exactly what Apple does, except that I push the UISearchContainerViewController onto a navigation stack, or I wrap it in a navigation controller and present it, or whatever; and there's no search field so the whole thing is useless.
Has anyone ever gotten UISearchContainerViewController to do anything useful on iOS?
I was looking for answer to the same question. But I got this working like that :
Base VC
let vc = SearchContainerVC()
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: true, completion: nil)
SearchContainerVC
class SearchContainerVC: UISearchContainerViewController {
init() {
let searchResultsTableVC = SearchResultsTableViewController()
let searchVC = UISearchController(searchResultsController: searchResultsTableVC)
searchVC.searchBar.searchBarStyle = .minimal
searchVC.searchBar.showsCancelButton = true
searchVC.searchBar.delegate = searchResultsTableVC
searchVC.searchResultsUpdater = searchResultsTableVC
let searchBar = searchVC.searchBar
searchBar.delegate = searchResultsTableVC
searchBar.sizeToFit()
searchBar.placeholder = "Search for something"
searchVC.hidesNavigationBarDuringPresentation = false
searchVC.dimsBackgroundDuringPresentation = true
super.init(searchController: searchVC)
navigationItem.titleView = searchBar
}
}
SearchResultsTableViewController
class SearchResultsTableViewController: UITableViewController {
// I use a Table to show search items
}

Top layout guide moves underneath navigation bar after pushing search results

This one has kept be up a few days. I think the easiest way to illustrate my issue is with this 10 second gif.
Description
As you can see, when you go back after pushing to a search result, the segmented top bar disappears when you go back, but when I switch to the tab with "Tise" in the header and back, it resets and works properly.
As seen in the below screenshot, my issue is that the top layout guide gets offset underneath the navigation bar, which makes the segmented bar hide underneath it.
Code
My search controller is an extremely normal implementation.
/* Search controller */
let exploreSearchController = StandaloneExploreSearchResultController.initFromStoryboard(type: .top)
searchController = UISearchController(searchResultsController: exploreSearchController)
searchController?.searchResultsUpdater = exploreSearchController
searchController?.delegate = self
searchController?.searchBar.searchBarStyle = .minimal
searchController?.searchBar.isTranslucent = false
searchController?.searchBar.backgroundColor = .clear
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.dimsBackgroundDuringPresentation = false
searchController?.searchBar.tintColor = #colorLiteral()
navigationItem.titleView = searchController?.searchBar
definesPresentationContext = true
and I push the result controllers in StandaloneExploreSearchResults with
presentingViewController?.navigationController?.pushViewController(viewController, animated: true)
For every new view controller, I update the navigation bar style, which I suspect might be triggering it. If I disable this function, I get different offset bugs, as seen in the following gif.
func updateNavigationBarStyle(viewController: UIViewController) {
let style = (viewController as? NavigationControllerStyling)?.preferredStyle() ?? .default
print(#file.components(separatedBy: "/").last!,":",#line,"-",#function, viewController.classForCoder)
//Showhide
UIApplication.shared.setStatusBar(hidden: style.statusBar.hidden, style: style.statusBar.style)
setNavigationBarHidden(style.hidden.hidden, animated: style.hidden.animated)
navigationBar.setCustomShadow(hidden: style.shadowHidden)
//Colors
navigationBar.tintColor = style.tintColor
navigationBar.barTintColor = style.barTintColor
navigationBar.backgroundColor = style.backgroundColor
navigationBar.setBackgroundImage(style.backgroundImage, for: .default)
navigationBar.titleTextAttributes = style.titleTextAttributes
navigationBar.isTranslucent = style.translucent
navigationBar.hairlineImageView?.isHidden = true //Always hide the native shadow
}
Question
Is there a way to either force the auto layout reload that switching tabs triggers (I've tried all the methods I could find with no success). or fix the bug somehow?
Well, that solved it. I guess setting the colors and/or images somehow fiddled with the opaque property.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
extendedLayoutIncludesOpaqueBars = true
}
Try this code in viewDidLoad -
self.edgesForExtendedLayout = []

Swift UISeachController add nav bar on selection

I currently have my UISearchController set up so it is presented over the nav bar once the search icon is clicked.
var searchController: UISearchController!
#IBAction func searchMe(sender: UIBarButtonItem) {
// Create the search results view controller and use it for the `UISearchController`.
let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier(SearchResultsViewController.StoryboardConstants.identifier) as! SearchResultsViewController
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = Colors.blue
searchController.searchBar.barTintColor = UIColor.whiteColor()
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
I am using a different searchResultsController than the current table view to display results. The problem is that when a cell is selected in the searchResultsController a segue is performed to a new view controller (the users profile) and there is no navigation bar.
I need a back button to appear to take the user back to the searchResultsController. I have tried a bunch of different segues, setting the nav bars on the storyboard but nothing seems to work. My initial thought was to make this line of code:
searchController = UISearchController(searchResultsController: searchResultsController)
point to a nav controller with searchResultsController as its root view controller but I had no luck. Any help is great! Thank you!

Add UISearchBar to UINavigationBar with UIBarButtonItem

In my app, I used to have a search bar in the header view of my table view. However, I have added a search bar button item, and when a user taps it, I want a search bar to animate across the navigation bar, and the user should be able to search. This is kind of like the search bar in the Twitter iOS app. here is the code for my UISearchController:
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = true
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
//self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
controller.searchBar.returnKeyType = .Search
controller.searchBar.delegate = self
return controller
})()
Here is how my UINavigationBar looks like right now:
How would I go about doing this?
You're almost there. You need to do four things:
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
hide all the current UIBarButtonItems you have in the navigation bar
searchController.searchBar.becomesFirstResponder()
That is, you setup your UISearchController so it doesn't hide the navigation bar, as the UISearchBar is gonna be displayed there.
Then, when your IBAction runs, you do something like:
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
navigationItem.rightBarButtonItem = nil
searchController.searchBar.becomeFirstResponder()
That guarantees the UISearchBar will use the entire UINavigationBar when it becomes the first responder. You also get the UISearchBar animation, which does a good job in hiding the unanimated removal of the UIBarButtonItems.
You will need to implement the UISearchBarDelegate (or if you prefer UISearchControllerDelegate) to restore the original navigation bar state once the user is done with the search.
One very important detail is that in order for your UISearchBar to be editable in the titleView, no other view controller can be set with definesPresentationContext = true at the time yours is pushed.

Resources