Search Controller .dimsBackgroundDuringPresentation also dims Status Bar? - swift

I have a function to set up my Search Controller which I add to View Did Load.
func setUpSearchController() {
searchController.delegate = self
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.sizeToFit()
searchController.searchBar.backgroundColor = .white
searchController.searchBar.barTintColor = .white
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = Colours.brandGreen
definesPresentationContext = true
messagesTableView.tableHeaderView = searchController.searchBar
}
When clicking on the Search Bar the Search Controller dims the background as expected by
searchController.dimsBackgroundDuringPresentation = true
The issue is it also dims the status bar making the UI look ugly. Anyone know how to exclude the status bar from also being dimmed?
As always any help greatly appreciated. Illustration below.
Update
I realise I'm using the Search Controllers Search Bar inside the TableView Header and changing this from
messagesTableView.tableHeaderView = searchController.searchBar
to
navigationItem.searchController = searchController
I get the desired result. However, I'd still like the Search Controllers Search Bar inside the Table Views header.

Related

UISearchBarSearchContainerView is black

Why it is black?
In viewDidLoad I set this code:
var searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
navigationItem.hidesSearchBarWhenScrolling = false
searchController.delegate = self
searchController.searchBar.delegate = self
Try to change the style of the search bar.
It will look like
searchController.searchBar.barTintColor = .blue
Here you can find how to change searchBar's appearance: https://developer.apple.com/documentation/uikit/uisearchbar/1624281-searchbarstyle
I had a similar problem when I set the app's "User Interface Style" to "Light" in info.plist. For some reason, the search bar doesn't appear to respect this setting and is rendering black when the device is in Dark Mode on iOS 13. I was able to force the search bar to render as white again by setting the search and navigation controllers' background colors.
searchController.searchBar.backgroundColor = .white
navigationController?.view.backgroundColor = .white

Pull to refresh issue using search bar and large title navigation bar

Having an issue while using large titles navigation bar and search bar in the navigation controller, when I try to pull for refreshing tableview shows an abrupt behavior on end refreshing.
var searchController : UISearchController!
self.searchController = UISearchController(searchResultsController: nil)
self.tableView.refreshControl = self.refreshControl
self.navigationItem.searchController = searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
self.definesPresentationContext = true
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchResultsUpdater = self
On end refreshing
I had the same issue, and the only way I managed to get it working is by manually setting tableView's contentOffset.
tableView.refreshControl?.endRefreshing()
tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

UISearchController searchBar move down when search is active

I have presented view controller, which has table view in it.
I have added UISearchController in table header view. When search bar is active, it move down some pixel.I have tried all the options, none are working.
Below my code to add search controller in table view
searchController = UISearchController.init(searchResultsController: nil)
searchController?.searchResultsUpdater = self
searchController?.searchBar.sizeToFit()
searchController?.searchBar.delegate = self
searchController?.searchBar.searchBarStyle = UISearchBarStyle.default
searchController?.dimsBackgroundDuringPresentation = false
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.definesPresentationContext = true
searchController?.searchBar.placeholder = "Search"
self.otlTableView?.tableHeaderView = searchController?.searchBar
I am also attaching screenshots.
Searchcontroller1
Searchcontroller2

UISearchBar alignment issues in iOS 11

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 strange

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
}