UISearchBarSearchField BackgroundView color - PullRequest
0 голосов
/ 27 сентября 2019

enter image description here Я пытаюсь изменить цвет фона текстового поля панели поиска, и я искал и пробовал множество решений, но они не работают.

Итак, пожалуйсталюбой может сказать мне точное решение для этого.Как мы можем изменить цвет фона текстового поля панели поиска?

/// used to prepare searchController inside navigation.
private func prepareNavigationSearchControllerSetup() {
    self.title = AppConstant.kContacts
    let search = UISearchController(searchResultsController: nil)
    search.searchResultsUpdater = self
    search.searchBar.cornerRadius = 10.0
    search.searchBar.textField?.backgroundColor = .red
    search.searchBar.textField?.tintColor = .yellow
    self.navigationItem.searchController = search
    self.navigationItem.hidesSearchBarWhenScrolling = false
}

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { $0.subviews }
        return (subViews.filter { $0 is UITextField }).first as? UITextField
    }
}

Ответы [ 2 ]

1 голос
/ 27 сентября 2019

Вы можете попробовать это

UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue

Выход

enter image description here

Редактировать: ПолныйКод

    var searchController = UISearchController()
    let resultsTableController = Storyboard.Home.instantiateViewController(withIdentifier: "GlobalTableVC") as! GlobalTableVC
    resultsTableController.tableView.delegate = self
    resultsTableController.tableView.dataSource = self

    resultsTableController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SearchCell")

    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchBar.placeholder = "Search"
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.sizeToFit()
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.keyboardType = UIKeyboardType.alphabet
    searchController.searchBar.tintColor = UIColor.white
    searchController.searchBar.barTintColor = UIColor(hexString: "EB033B")

    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue


    searchController.searchBar.delegate = self
    searchController.delegate = self
    searchController.searchResultsUpdater = self

    present(searchController, animated: true, completion: nil)
0 голосов
/ 30 сентября 2019

После долгих поисков я нашел правильный ответ, который мне подходит.

if #available(iOS 11.0, *) {
        if let textfield = search.searchBar.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.blue

            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;
            }
        }

  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...