UISearchBarSearchContainerView is black - swift

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

Related

How to achieve the auto-shrink SwiftUI Navigation Title with UIKit?

I am trying to achieve the auto-shrink large nav title like the Navigation Title in SwiftUI, but no matter what my nav title always stay shrinked and never appear in large title mode. How do i fix this? (code below)
func setup() {
table.translatesAutoresizingMaskIntoConstraints = false
table.dataSource = self
table.delegate = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
table.separatorColor = .white
table.backgroundColor = .black
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
appearance.configureWithOpaqueBackground()
navigationItem.title = "Todo List"
navigationItem.scrollEdgeAppearance = appearance
navigationItem.standardAppearance = appearance
view.addSubview(table)
}
As well as providing a configuration for large titles, you need to instruct your navigationController to use large titles
navigationController.navigationBar.prefersLargeTitles = true
The default configuration for a view controller should be to automatically use large titles if that is the preferred choice. However you can set it explicitly per view controller:
navigationItem.largeTitleDisplayMode = .always // or .automatic or .never
If the nav controller has prefersLargeTitle = false then large titles will never be displayed whatever the largeTitleDisplayMode setting.

Need to show navigation title while using searchBar in navigationBar

I need to show large navigation title while search with searchBar, but when I'm clicking on Searchbar or starting to type my NavigationTitle is replaced with SearchBar. I checked methods for navigationItem and didn't find any suitable.
Does anybody know how can we always show title, even while searching?
Here is my code
class FavouriteVC: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Favourite"
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .always
}
}
And here are shots
screen with NavTitle
screen without NavTitle
Seems like UISearchController has a hidesNavigationBarDuringPresentation that prevents the navigation bar from hiding when set to false
Include:
searchController.hidesNavigationBarDuringPresentation = false
in your viewDidLoad and if I've understood your issue right, this should do what you need.

How to change the background color of navigationBar

I am trying to change the background color of the navigation bar to black but without success. I have checked the answer to this related question but none of the solutions have worked. This is what I have tried:
navigationController?.navigationBar.backgroundColor = UIColor.black
UINavigationBar.appearance().backgroundColor = UIColor.black
You can also change the navigation bar color for a specific view controller, this way:
extension UIViewController {
func setCustomNavigationColor(color: UIColor = .black, isTranslucent: Bool = false ){
self.navigationController?.navigationBar.barTintColor = color
self.navigationController?.navigationBar.isTranslucent = isTranslucent
}
}
call this from viewDidLoad()
setCustomNavigationColor()
Try this:
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().isTranslucent = false
Note: This changes the bar colour for the entire app.

Search Controller .dimsBackgroundDuringPresentation also dims Status Bar?

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.

White line under navigation bar when scrolling or until pressing on the search bar

As you can see in the video, there is a white thin line under the navigation bar the moment i start scrolling.
It would only disappear when I would press on the searchBar (contained by my searchController, so the search bar is not added from the Storyboard). I tried a lot of different combinations in order to try to make it disappear but nothing worked.
Any help is appreciated! Thanks!
Video: https://www.youtube.com/watch?v=KcgZmBg1VS0
This is the code inside my viewDidLoad:
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = "Search for a recipe"
searchController.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
searchController.searchBar.tintColor = UIColor.white
Try to set background color and change shadow of the navigationBar. It possibly connected with navigationBar.
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
Try to add this line:
searchController.searchBar.backgroundColor = navigationController?.navigationBar.barTintColor
In the file setupSearchBar()
After code fix:
You can remove that shadow below your navigation bar by the way. I have created extension for doing this:
extension UINavigationBar {
func shouldRemoveShadow(_ value: Bool) -> Void {
self.setValue(value, forKey: "hidesShadow")
}
}