I have a search controller in my app which according to apple documentation on iOS 11 has to be added to the navigationItem's searchController property. I also have some scope buttons for the searchController. Everything works fine but when I rotate my device to landscape and rotate back to portrait then a wired thing happens. The scope buttons gets hidden under the search field. If I press the cancel button and tap on the search field again then it comes back to the normal state. The bug reproduces everytime only on iOS 11. The searchbar will look like this:
The normal state of the search controller:
This is how I add the searchController:
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search device"
searchController.searchBar.scopeButtonTitles = ["Device", "Person", "OS version", "Serial"]
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor.white
backgroundView.layer.cornerRadius = 10
backgroundView.clipsToBounds = true
}
}
} else {
tableView.tableHeaderView = searchController.searchBar
}
searchController.searchBar.delegate = self as UISearchBarDelegate
Related
I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?
Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}
I am developing an TVOS app. I use below code to implement search feature in UITabBarController. The issue when tabbar is displayed then search bar is covered by tabbar. See below screenshots. is it possible to move searchBar position down if tabbar gets displayed?
let searchViewController = VVSearchViewController()
let searchController = UISearchController(searchResultsController: searchViewController)
searchViewController.searchViewController = searchController
searchController.searchResultsUpdater = searchViewController
searchController.obscuresBackgroundDuringPresentation = true
let searchPlaceholderText = "Search"
searchController.searchBar.placeholder = searchPlaceholderText
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.barTintColor = UIColor.black
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.keyboardAppearance = .dark
searchController.view.backgroundColor = UIColor.black
searchController.definesPresentationContext = true;
UISearchContainerViewController(searchController: searchController)
It is possible.
It took me a while but my idea was to add the search controller into a "container" controller with constraint at the top. Nothing will appear if you try adding UISearchController as child view controller into the container. UISearchContainerViewController doesn't work as well. But a navigation controller with UISearchContainerViewController as root works. Here is my code:
// Create the search controller and it's results controller
let resultsController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchResultsViewController") as! SearchResultsViewController
let searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController
// Customize it
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [.foregroundColor: UIColor.text]
searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController.searchBar.tintColor = .text
searchController.searchBar.barTintColor = .text
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.keyboardAppearance = .dark
// Embed it in UISearchContainerViewController
let searchContainerViewController = UISearchContainerViewController(searchController: searchController)
// Make UISearchContainerViewController root controller of a navigation controller
let navController = UINavigationController(rootViewController: searchContainerViewController)
// And a container to hold everything
let containerViewController = UIViewController()
// Do a standard child add of navController into containerViewController
navController.loadViewIfNeeded()
navController.viewWillAppear(false)
navController.willMove(toParent: containerViewController)
containerViewController.view.addSubview(navController.view)
navController.view.frame = containerViewController.view.bounds
// Add your constraints. In this case I just want the search bar to be 150 px down.
NSLayoutConstraint.activate([
navController.view.topAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.topAnchor, constant: 150),
navController.view.leftAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.leftAnchor),
navController.view.bottomAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.bottomAnchor),
navController.view.rightAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.rightAnchor)
]) containerViewController.addChild(navController)
containerViewController.addChild(navController)
navController.didMove(toParent: containerViewController)
navController.viewDidAppear(false)
// And from here use the containerViewController as normal to display a search into your app.
Note: Without passing the viewDidAppear on the navController this will not work.
Note 2: I tested this on tvOS 13 and 14 only.
You can find exactly the same case in the tab Search of the application "App Store".
This is because in tvOS the content of a viewController is not supposed to react to the visibility of the tab bar. The tab bar is presented instead on top of it, overlapping.
tvOS Human Interface GuideLines for Tab Bars:
https://developer.apple.com/tvos/human-interface-guidelines/interface-elements/tab-bars/
How do I adjust the constraint when pushing a new UIViewController when a UISearchController is implemented as self.navigationItem.searchController = search
New controller pushed: SearchBar collapses but leaves View with constrait to top = 0 on the initial position.
(The background of the ViewController is yellow for easy understanding of the matter)
Implementation:
viewDidLoad() {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.definesPresentationContext = true
search.hidesNavigationBarDuringPresentation = false
search.searchBar.tintColor = UI.COLOR_WHITE
search.dimsBackgroundDuringPresentation = false
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.searchController = search
}
I have tried using self.automaticallyAdjustsScrollViewInsets = false without success and the tableView implements self.contentInsetAdjustmentBehavior = .always
What am I missing? Help is very appreciated.
I have UITableViewController where i was adding UISearchController searchbar as an headerView and I have changed this to navigationItem.searchcontroller for iOS 11 in iOS 11 searchbar won't appear at all even navigationItem was present.
if don't use this searchbar UI is going for toss. Please suggest me what was i am doing wrong here.
let searchController = UISearchController(searchResultsController: vc)
searchController.searchBar.scopeButtonTitles = ["All", "images", "videos", "others"]
searchController.dimsBackgroundDuringPresentation = false
// Don't remove these two lines then search bar alignment issue will raise
self.extendedLayoutIncludesOpaqueBars = true
self.searchController.searchBar.isHidden = false
definesPresentationContext = false
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController // not working for me.
searchController?.hidesNavigationBarDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
} else {
searchController?.hidesNavigationBarDuringPresentation = true
searchController?.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
List of issues are
on iOS 11 with UI goes wrong as attached image if add UISearchbar to header view
if I try add to navigation item as mentioned in the code it is not appearing
As i am using different search results controller when search is active i need to disable actions in present controller - how can i achieve this easily.
I'm not really sure what went wrong there but in my case, it's working fine. Here is my code:
Created a searchController file:
private let searchController = UISearchController(searchResultsController: nil)
In viewDidLoad():
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = kSearchPlaceholder
searchController.searchBar.scopeButtonTitles = [kCategory, kAuthor, kPicture]
searchController.searchBar.delegate = self
definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
And it's good to go.
After updating to iOS11 my searchBar behaves very strange. When activated, it jumps to the top of screen. It works as it should, as before but of course I want it to stay in place. I have tried a lot of solutions from googling this behavior but nothing helps. Do anyone have the same problem? And what did you do to solve this?
searchBar in place
searchBar jumped to top
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.preservesSuperviewLayoutMargins = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
walkaboutTableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = matildaLightBlue
searchController.searchBar.clipsToBounds = true
searchController.searchBar.layer.borderWidth = 2
searchController.searchBar.layer.borderColor = UIColor.black.cgColor
}
Adding search controller instance into navigation item instead of table header view helps me to solve similar issue in my project.
if #available(iOS 11, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
navigationItem.searchController
navigationItem.hidesSearchBarWhenScrolling
Ruslan Kolosovskyi's answer is really good. In addition if you want the search bar to be more accessible:
if #available(iOS 11, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}