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
}
Related
Hello I am trying to implement a SearchController into my application however, my application has different tabs in which I would like them to see the search functionality. I have added a button to the NavBar and each of these tabs has a main controller view which includes this code. Unfortunately when I try to search for something the table view does not display with the results. Thank you for your help! Please find the code below:
MainController:
// set up search:
func setUpSearch(){
self.searchDataSource = DataTableViewController() // UIViewController, contains all functions for tableview plus outlet of tableview
self.searchController = UISearchController.init(searchResultsController: searchDataSource)
self.searchController?.searchBar.delegate = self
self.searchController?.searchBar.showsCancelButton = true
self.searchController?.searchBar.placeholder = "Search for people or places"
}
// user clicked on search button:
func searchButtonClicked(){
self.searchController?.isActive = true
self.searchController?.searchBar.becomeFirstResponder()
if let controller = self.searchController {
present(controller, animated: true, completion: nil)
}
}
When I try to present a TabViewController, I get odd behavior from both my TabBar and NavigationBar as seen in the images below. It stays as shown in the "before" image until I touch the screen or push a button. At the point it jumps to the "after" image.
Before:
After:
Code used to present the TabViewController:
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate.tabViewController = TabViewController()
self.present(delegate.tabViewController!, animated: true, completion: nil)
Initialization of the TabViewController:
override func viewDidLoad() {
super.viewDidLoad()
let groupTable = GroupTableViewController()
let nav = UINavigationController(rootViewController: groupTable)
nav.title = "Groups"
nav.tabBarItem.image = UIImage(named: "groups")
let vc2 = MeViewController()
vc2.title = "Me"
vc2.tabBarItem.image = UIImage(named: "user")
// let vc3 = SettingsViewController
// vc3.title = "Settings"
// vc3.tabBarItem.image = UIImage(named: "settings")
self.viewControllers = [nav, vc2]
self.selectedIndex = 0
}
Console log, but I don't think the error is relevant:
objc[63765]: Class PLBuildVersion is implemented in both /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11916f998) and /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x118069d38).
One of the two will be used. Which one is undefined.
This is a new bug I've been experiencing seemingly after updating to Xcode 8.1/MacOS Sierra.
My XCode version is Version 8.1 beta (8T47). Could this be a bug in the beta?
I'm unsure what is causing this as I didn't make a code change when this started happening.
Thanks for the help.
The viewDidLoad of the tab view controller is really too late to be configuring the tab view controller with its two child view controllers. Either do this in the "Code used to present the TabViewController", or, if you really want to do it from within the tab view controller itself, do it from the tab view controller's initializer. Then all will be well.
I have a problem with search bar. I need to create a table view with a search button in the right corner, and when I click on it, it should show the search bar.
My code is here:
// Search controller
searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchBar.delegate = self
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.sizeToFit()
return controller
})()
And here is action:
// Search action
#IBAction func search(sender: UIBarButtonItem) {
print("Open search")
searchController.active = true
if searchController.searchBar.isFirstResponder() == false {
searchController.searchBar.becomeFirstResponder()
}
}
When I click on the button, nothing happens (only prints text in console), and what I want is in the image below:
Your class needs to conform to a UISearchBarDelegate, I'm not sure if you've done that already. Also make sure you present the search view
From Apple's Docs:
The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the text field.
Here's a sample from my app
#IBAction func searchAction(sender: UIBarButtonItem) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)
}
Old question, but want to add a new answer (maybe things changed since the original answer from 2016).
Simply call this on the view controller, assuming you already set up the search controller. No need to conform to UISearchBarDelegate:
Swift 5
present(searchController, animated: true, completion: nil)
I know this question gets asked a lot.
I've tried every answer (I think) and can't get a title to show up.
I have a static method I use to show view controllers modally.
I think I understand that a navigation controller gets its title from the title property of the view controller it is presenting, but I cannot get my vc's title to show up, and other methods I have tried aren't working, either.
static func present(vc vc: UIViewController) {
// The navCtrl presents a vc, and uses that title.
// I should be able to set this, right?
vc.title = "vc.title" // doesn't show up
vc.navigationItem.title = "vc.navigationItem.title" // nothing
vc.navigationItem.prompt = "vc.navigationItem.prompt" // doesn't show
vc.navigationController?.title = "vc.navigationController?.title" // nuttin'
let navCtrl = UINavigationController(rootViewController: vc)
navCtrl.navigationBar.tintColor = UIColor.whiteColor()
navCtrl.navigationBar.barTintColor = UIColor.blackColor()
navCtrl.navigationBar.translucent = false
// How about adding a UINavigationItem? Nope.
// let item = UINavigationItem(title: "UINavigationItem")
// navCtrl.navigationBar.pushNavigationItem(item, animated: true) // CRASH: Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.
// Can I set something on the navCtrl? Huh-uh. None of this works
navCtrl.navigationController?.title = "navCtrl.navigationController?.title"
navCtrl.navigationItem.prompt = "navCtrl.navigationItem.prompt"
navCtrl.title = "navCtrl.title"
navCtrl.navigationItem.title = "navCtrl.navigationItem.title"
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navCtrl, animated: true, completion: nil)
}
No matter what I've tried, I just get an empty navigation bar with a close button:
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