Search Controller in navigation item show black bar - ios

Hi I have setup search controller using
private func setupSearchbar() {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Contacts"
self.navigationItem.searchController = searchController
self.definesPresentationContext = true
}
Everything was working fine until I add this code
UINavigationBar.appearance().isOpaque = true
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = UIColor(named: "PrimaryDark")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font:Constants.AppTheme.Fonts.font(type: .FONT_BOLD, size: 22) ,NSAttributedString.Key.foregroundColor:UIColor.white]
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.font:Constants.AppTheme.Fonts.font(type: .FONT_BOLD, size: 34) ,NSAttributedString.Key.foregroundColor:UIColor.white]
Now When I tap on search bar big bottom black bar appears
How to fix this ?
View Debugging

Okay So I am able to fix this issue. I am sharing this so this might helpful to other facing the same issue.
to fix this issue. In view will appear method I have set self.extendedLayoutIncludesOpaqueBars to true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.extendedLayoutIncludesOpaqueBars = true
}
Hope it helps to someone :)
Here is output

Remove this
UINavigationBar.appearance().isOpaque = true
UINavigationBar.appearance().isTranslucent = false

Related

Adding in SearchController causes Navigation Bar to be black

I am adding in a UISearchController in my navigation controller and when I do, the entire navigation bar goes black, instead of the color I want. It does go to the desired color when the I click on the search bar however.
Before calling setupSearch()
After calling setupSearch()
class SearchViewController: UIViewController, UISearchBarDelegate, UISearchControllerDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
delegate()
create()
setupNav()
setupSearch()
}
func delegate() {
searchController.delegate = self
searchController.searchBar.delegate = self
}
func create() {
definesPresentationContext = true
view.backgroundColor = .backgroundColor()
}
func setupNav() {
guard let navController = navigationController else { return }
navController.navigationBar.prefersLargeTitles = false
navController.navigationBar.barTintColor = .secondaryColor()
navController.navigationBar.isTranslucent = false
navController.navigationBar.topItem?.title = "Browse"
navController.navigationBar.titleTextAttributes = [.font: UIFont.customHeaderFont(size: 23), .foregroundColor: UIColor.textColor()]
}
func setupSearch() {
let searchBar = searchController.searchBar
searchBar.backgroundImage = UIImage()
searchController.extendedLayoutIncludesOpaqueBars = true
navigationItem.searchController = searchController
}
}
if you give color to barTintColor and set isTranslucent to false,setting navigationItem.searchController will cause the bar to turn black
Solution 1, if you want to always display the searchbar
navigationItem.hidesSearchBarWhenScrolling = false
Solution 2, set navigationController backgroundColor
navigationController?.view.backgroundColor = .red
Change the background color of the View (outermost) that contains the safeArea, that works for me. The reason that you are seeing a black color is that you did not assign any color to the View background so the default one is assigned

How do I display a UISearchController on a UINavigationItem without an underlying UIScrollView?

Maybe this is impossible, but I always assumed you could just throw a UISearchController instance onto any old view controller's navigationItem and get a search bar. It seems to me like no matter what I try, I can't get it to work. It's making me think this behavior is hardcoded to only work when the view controller's view property is a subclass of UIScrollView.
I hope this is just a red herring. If I missed something obvious, please help! This is infuriating.
Here's what I did:
import UIKit.UIViewController
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = {
let searchController = UISearchController()
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
}
}
No search bar ever appears on screen. It just looks like a regular old navigation bar.
The UISearchController initializer should be let searchController = UISearchController(searchResultsController: nil) or replace nil with a seperate controller to display the search results.
If your viewController is in a UINavigationController stack then the above code should work (with the corrected initializer). Otherwise you will need to create a UINavigationBar and add it to the view. Then add the searchController.searchBar to the navigationItem.titleView
let navigationBar = UINavigationBar()
view.addSubview(navigationBar)
navigationBar.barTintColor = UIColor.gray
navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
navigationBar.delegate = self
navigationBar.items = [navigationItem]
navigationItem.searchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
navigationItem.titleView = navigationItem.searchController?.searchBar

Search bar not showing up on the navigation bar iOS 11

I am pushing a viewController where I want a searchBar, but search bar is not showing at all. Below is the code. Am I missing something?
var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
navigationItem.titleView?.layoutSubviews()
}
You need to add this line to your code:
navigationItem.hidesSearchBarWhenScrolling = false
That removes hiding searchBar while scrolling and shows it on pushing your view controller.
So, navigationItem.hidesSearchWhenScrolling only works when you set the searchController property of navigationItem NOT when you set the navigationItem.titleView to searchBar.

UISearchController in UITabBarController - strange white line appearing when NavBar collapses

I can't figure out why there is this white line appearing when I click in the search text field. The only way to prevent it from happening is to make the navigationBar.isTranslucent = false, but this is not an ideal solution. Does anyone know why this might be happening and how to fix it?
class SearchViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "Search..."
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
...
}
Update
I've determined that the UITabBarController is to blame for this annoyance, but I don't know why or how to fix it.

Position of UISearchBar moving down when clicked on iOS10

I have a trouble with a search bar only on iOS 10 (it works well on iOS 11) and I don't know what's going on, it's been several days I'm trying to solve this by myself without success so I hope someone here could tell me what I do wrong.
So, initially, my screen is like this
And when I click in the search bar, it goes like that
If I type text, it well displays the results under the search bar. I just don't know where this space between my search bar and the navigation bar comes from.
The results of my search is managed by an another viewcontroller declared like this.
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTableViewController") as! LocationSearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
A bit of my code, especially where I set up the search bar:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.delegate = self
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.isTranslucent = false
resultSearchController?.dimsBackgroundDuringPresentation = false
mainView.addSubview(searchBar)
}
definesPresentationContext = true
resultSearchController?.hidesNavigationBarDuringPresentation = false
}
For others who might encounter the same issue, here is my code now and it's working.
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.barStyle = .default
searchBar.isTranslucent = false
resultSearchController?.hidesNavigationBarDuringPresentation = true
resultSearchController?.dimsBackgroundDuringPresentation = false
UI_mapView.addSubview(searchBar)
}
definesPresentationContext = true
let currentLocationButtonItem = MKUserTrackingBarButtonItem(mapView: UI_mapView)
self.navigationItem.rightBarButtonItem = currentLocationButtonItem

Resources