First time this part of the application loads the search controller's search bar's cursor shows, as desired.
(The Problem) When search is dismissed and then (later) re-laoded, no cursor:
This is repeatable with just loading/dismissing, i.e. nothing that apparently could/should change the tint of things, causing a cursor to be the color of the background (as is typically the answer to missing cursor, from SO Q&As I've seen on the topic.) That said, nothing else fails to work, it is just the cursor that disappears.
Some (possibly) complicating factors:
I have made various attempt to have the child VC create it's own SearchController, and/or use one on the root VCs, and so forth. The various permutations seem to work, but with this same flaw. (As such, I feel I'm looking in the wrong place for the source of the problem/solution.)
I cannot rule out the the cursor color has changed, I don't fully understand how it is set/inherited, especially given the VC stack I have, nor how to test it in a debugger. I think it is as simple as I set a global tint.
Note: This app is using storyboard with direct settings, and is not using appearance proxies much. That said it has:
// Default tint for application...
UIApplication.sharedApplication().delegate?.window??.tintColor = mainBrandColor
UIToolbar.appearance().tintColor = mainBrandColor
... and I've tried with various permutations of UISearchBar, UINavigationBar tints via appearance proxy w/ same behavior (first works, subsequent not so much.) Looking at this, at various times, (i.e. when working and when not) it shows the same color:
(lldb) po searchController.searchBar.tintColor
Note: A separate use of UISearchController (when the calling VC is not a child of the container, but a pushed VC) does NOT demonstrate this problem. The cursor remains the correct color.
Environment: This is an iOS9.x application in Swift on XCode 7.1.
Here is some code, where homeVC is the parent/container VC:
if nil == homeVC.searchController {
homeVC.searchController = UISearchController(searchResultsController: nil)
homeVC.searchController!.searchResultsUpdater = self
homeVC.searchController!.delegate = self
homeVC.searchController!.searchBar.delegate = self
homeVC.searchController!.searchBar.showsCancelButton = false
homeVC.searchController!.searchBar.returnKeyType = .Done
homeVC.searchController!.searchBar.placeholder = "Add Item"
homeVC.searchController!.searchBar.searchBarStyle = UISearchBarStyle.Minimal
homeVC.searchController!.dimsBackgroundDuringPresentation = false
homeVC.searchController!.hidesNavigationBarDuringPresentation = false
}
homeVC.navigationItem.titleView = homeVC.searchController!.searchBar
I've tried with and without lazy loading, and with destroying / re-creating and not.
Any pointers/thoughts on where to look / how to troubleshoot would be be appreciated.
Are you doing
self.searchController.searchBar.showsCancelButton = NO;
I had exactly the same problem and it turns out that this thing is somehow related. Try to comment it out and see if the cursor is back.
I was having the same problem. The trick that worked for me was setting the tintColor of the UISearchBar when it becomes the firstResponder
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
searchBar.tintColor = UIColor.redColor()
return true
}
I had a big headache with this same problem. user1491604 (the first answer) is correct that the cause of the problem is:
searchController.searchBar.showsCancelButton = false //Swift 3
Commenting it out completely fixed the problem but then I ran into what to do if I DIDN'T want to show the Cancel button at all like below:
If you don't want to show the cancel button at all then user1491604's answer won't work. I followed Vitya Shurapov's answer (which works in this situation) to implement my own SearchController:
https://stackoverflow.com/questions/29976503/uisearchcontroller-searchbar-showscancelbutton-not-being-respected/42030000#42030000
class CustomSearchBar: UISearchBar {
override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
super.setShowsCancelButton(false, animated: false)
}
}
class CustomSearchController: UISearchController {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let customSearchBar = CustomSearchBar(frame: CGRect.zero)
return customSearchBar
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Then to use it in viewDidLoad I used:
let searchController = CustomSearchController(searchResultsController: nil)
//...configure the rest of the searchController...
//DELETE- searchController.searchBar.showsCancelButton -you can delete this line wherever your using it
Problem solved for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With