1

I have searched the similar question UISearchController iOS 11 customizationbut al the methods in the comments couldn't help me out.So I want to ask it again.

I used the following codes to set the appearance of the searchBar.

Extension the UISearchBr to get the textField and placeHolderLabel:

extension UISearchBar{

    var textField: UITextField?{
        if let textField = self.value(forKey: "searchField") as? UITextField {

           return textField
        }
        return nil
    }

    var placehloderLabel:UILabel?{

        if let placeholderLabel = textField?.value(forKey: "placeholderLabel") as? UILabel{
            return placeholderLabel
    }
        return nil
    }
}

Customize a subclass of UISearchController:

class CustomSearchController:UISearchController{

    override init(searchResultsController: UIViewController?) {

        super.init(searchResultsController: searchResultsController)
        self.definesPresentationContext = true
        self.dimsBackgroundDuringPresentation = false
        self.hidesNavigationBarDuringPresentation = true
        self.searchBar.searchBarStyle = .minimal

        self.searchBar.placeholder = "搜索歌单内歌曲"
        self.searchBar.textField?.textColor = UIColor.white
        self.searchBar.placehloderLabel?.textColor = .white
        self.searchBar.placehloderLabel?.font = UIFont.systemFont(ofSize: 15)

    }

Set prefersLargeTitles UINavigationBar.appearance().prefersLargeTitles = true

If navigationItem.searchController = searchController and the result is as follows (the appearance of the searchBar DOESN'T make change): enter image description here

But if I set navigationItem.titleView = searchController.searchBar,it makes: enter image description here

Does the iOS 11 permit developers to change the searchBar's appearance?If yes,I wonder how to customize it ?Any point is appreciated.Thanks!

  • Try: `UISearchBar.appearance.tintColor = UIColor.white;` or `UITextField.appearanceWhenContainedIn(UISearchBar.self, nil). setDefaultTextAttributes([NSForegroundColorAttributeName: UIColor.white]);` in `init` or `viewDidLoad` – Brandon Sep 01 '17 at 00:31
  • @Brandon I use UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white] and UISearchBar.appearance().tintColor = UIColor.white but it doesn't make sense. Anyway, thanks for your point. –  Sep 01 '17 at 01:15
  • It doesn't work? – Brandon Sep 01 '17 at 01:18
  • @Brandon yeah,does not work. –  Sep 01 '17 at 01:19
  • 1
    Surprise! I put`UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]` and `UISearchBar.appearance().tintColor = UIColor.white` in the `AppDelegate` Class, and it works. –  Sep 01 '17 at 01:43

1 Answers1

1

Working with color and UINavigationItem's search controller

This code is in the AppDelegate class.

UISearchBar.appearance().tintColor = UIColor(named: "Button") // using this to set the text color of the 'Cancel' button since the search bar ignores the global tint color property for some reason

if #available(iOS 11.0, *) {

      // Search bar placeholder text color
      UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search", attributes: [NSForegroundColorAttributeName: UIColor.white])

     // Search bar text color
      UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.red]

     // Insertion cursor color
      UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = UIColor.red

        } else {
            // Fallback on earlier versions
        }

   // Search bar clear icon
      UISearchBar.appearance().setImage(UIImage(named: "clear"), for: .clear, state: .normal)
Ringo
  • 1,173
  • 1
  • 12
  • 25