Why Is Uisearchcontroller Changing the Navigation Bar Colors

Why is UISearchController changing the navigation bar colors?

However when scrolling down and the search bar is displayed the background color of the navigation bar is lost

All of that is normal. New in iOS 13, the expanded nav bar (displaying search bar, large title, etc.) has different appearance from the normal nav bar. Your settings applied only to the normal nav bar because you didn't make them the iOS 13 way. If you want the expanded nav bar to look like the normal nav bar, you have to set its appearance separately and explicitly.

To do so, you need to set the navigation bar's scrollEdgeAppearance. Investigate classes UIBarAppearance, UINavigationBarAppearance, and UIBarButtonItemAppearance (you will need to set the backButtonAppearance explicitly).

Navigation bar becomes white when a UISearchController is added to it

It looks like it is required to use the new UINavigationBarAppearance on iOS 13. Try to add this to your viewDidLoad:

let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemRed
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

You will probably also want to set the searchField backgroundColor:

let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground

searchbar changes color when in navigation bar

Need to set UISearchBar tint color.

UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, 100, 20)];
[search setBarTintColor:[UIColor clearColor]];
search.backgroundImage=[UIImage new];
self.navigationItem.titleView=search;

Now navigation bar looks like below image :

Sample Image

Change UINavigationBar Background Color of UISearchController

You can use the barTintColor property, in Swift this would be:

searchBar.barTintColor = UIColor.redColor()

UISearchBar color in NavigationBar

Maybe the best solution is for creating an extension for UISearchBar if you want to use these settings in more controllers. Here are some example.

extension UISearchBar {

private func getViewElement<T>(type: T.Type) -> T? {

let svs = subviews.flatMap { $0.subviews }
guard let element = (svs.filter { $0 is T }).first as? T else { return nil }
return element
}

func getSearchBarTextField() -> UITextField? {
return getViewElement(type: UITextField.self)
}

func setTextColor(color: UIColor) {

if let textField = getSearchBarTextField() {
textField.textColor = color
}
}

func setTextFieldColor(color: UIColor) {

if let textField = getViewElement(type: UITextField.self) {
switch searchBarStyle {
case .minimal:
textField.layer.backgroundColor = color.cgColor
textField.layer.cornerRadius = 6
case .prominent, .default:
textField.backgroundColor = color
@unknown default:
print("something")
}
}
}

func setPlaceholderTextColor(color: UIColor) {

if let textField = getSearchBarTextField() {
textField.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes: [NSAttributedString.Key.foregroundColor: color])
}
}

func setTextFieldClearButtonColor(color: UIColor) {

if let textField = getSearchBarTextField() {

let button = textField.value(forKey: "clearButton") as! UIButton
if let image = button.imageView?.image {
button.setImage(image.transform(withNewColor: color), for: .normal)
}
}
}

func setSearchImageColor(color: UIColor) {

if let imageView = getSearchBarTextField()?.leftView as? UIImageView {
imageView.image = imageView.image?.transform(withNewColor: color)
}
}
}

Update:

Change navigationItem.searchController = sc to navigationItem.titleView = sc.searchBar.
Sample Image

UISearchController changing status bar color on invocation

Setting View-controller based status bar appearance to No is not a solution if you want the view controllers to define how the status bar looks.

My solution consisted of two things:

  1. Make sure the presenting view controller has definesPresentationContext set to YES
  2. Make sure both the view controller that is pushed and the pushing view controller are laid out beneath the navigation bar (set extendedLayoutIncludesOpaqueBars to YES)


Related Topics



Leave a reply



Submit