swift UISearchBarSearchField背景视图颜色

3pvhb19x  于 2023-11-16  发布在  Swift
关注(0)|答案(4)|浏览(103)


我试图改变搜索栏文本字段的背景颜色,我已经搜索并尝试了很多解决方案,但这些都不起作用。
所以,请任何人都可以告诉我确切的解决方案。我们如何改变搜索栏文本字段的背景颜色?

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

字符串

wgeznvg7

wgeznvg71#

经过多次搜索,我找到了适合我的正确答案。

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

  }

字符串

mrphzbgm

mrphzbgm2#

你可以试试这个

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)

qyyhg6bp

qyyhg6bp3#

自iOS 13起:

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

字符串

6za6bjd0

6za6bjd04#

对于iOS 17,唯一对我有效的是SuperDuperTango的答案here,我做了以下操作来移除那个覆盖。

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

字符串
然后重新应用拐角半径

searchBar.searchTextField.layer.cornerRadius = 8.0

相关问题