NavigationBar 中的 UISearchBar 颜色

UISearchBar color in NavigationBar

我觉得我在吃疯狂的药。我想更改 UISearchBar 中文本字段的颜色,它位于 UINavigationBar 中。我遇到的问题是 UISearchBar 获得了 UINavigationBar 的颜色,但 TextField 没有改变颜色。我在这里尝试了几种解决方案,但其中 none 似乎可以解决问题。关于如何更改 TextField 的颜色有什么想法吗?

我知道还有其他类似的问题,但我已经尝试了所有可能的解决方案,但没有找到解决我的问题的方法。

当前代码:

        let sc = UISearchController(searchResultsController: nil)
        sc.delegate = self
        let scb = sc.searchBar
        scb.tintColor = UIColor.green
        scb.barTintColor = UIColor.yellow


        if let textfield = scb.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.blue
            if let backgroundview = textfield.subviews.first {

                // Background color
                backgroundview.backgroundColor = UIColor.red

                // Rounded corner
                backgroundview.layer.cornerRadius = 5;
                backgroundview.clipsToBounds = true;

            }
        }

        navigationController?.navigationBar.barTintColor = UIColor.blue

        navigationItem.searchController = sc
        navigationItem.hidesSearchBarWhenScrolling = false

我创建了一个 sample project 来展示它的实际效果。任何帮助或指导将不胜感激!

如果您想在更多控制器中使用这些设置,也许最好的解决方案是为 UISearchBar 创建一个扩展。这里有一些例子。

extension UISearchBar {

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

        let svs = subviews.flatMap { [=10=].subviews }
        guard let element = (svs.filter { [=10=] 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)
        }
    }
}

更新:

navigationItem.searchController = sc 更改为 navigationItem.titleView = sc.searchBar