UISearchBarSearchField 背景视图颜色

问题描述 投票:0回答:5

我正在尝试更改搜索栏文本字段的背景颜色,我已经搜索并尝试了很多解决方案,但这些都不起作用。

所以,请任何人都可以告诉我确切的解决方案。我们如何更改搜索栏文本字段的背景颜色?

/// 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
    }
}
ios swift ios11
5个回答
3
投票

经过更多搜索后,我找到了对我有用的正确答案。

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;
            }
        }

  }

1
投票

你可以试试这个

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

输出

编辑:完整代码

    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
投票

自 iOS 13 起:

        if #available(iOS 13.0, *) {
            searchController.searchBar.searchTextField.backgroundColor = .red
            searchController.searchBar.searchTextField.tintColor = .yellow
        }

0
投票

对于 iOS 17,唯一对我有用的是 SuperDuperTango 的答案 here,我执行了以下操作来删除该覆盖层。

searchBar.setSearchFieldBackgroundImage(UIImage(), for: .normal)

然后重新应用圆角半径

searchBar.searchTextField.layer.cornerRadius = 8.0

0
投票

也许将来有人还需要它,我尝试了这个代码并且它有效!

if let searchTextField = searchBar.value(forKey: "searchField") as? UITextField {
    searchTextField.backgroundColor = UIColor.green // Change this to the desired color
}
© www.soinside.com 2019 - 2024. All rights reserved.