在NavigationBar中设置UISearchBar的颜色

3

我感觉自己像是疯了一样。我想要改变一个位于UINavigationBar中的UISearchBar中的textfield的颜色。问题在于,UISearchBar会继承UINavigationBar的颜色,但是textfield的颜色没有改变。我已经尝试了几种解决方案,但似乎都不能解决问题。有什么办法可以改变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

我创建了一个示例项目TestSearchBar来展示它的功能。非常感谢您的任何帮助或指导!
1个回答

1
也许最好的解决方案是为 UISearchBar 创建一个扩展,如果你想在更多的控制器中使用这些设置。以下是一些示例。
extension UISearchBar {

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

        let svs = subviews.flatMap { $0.subviews }
        guard let element = (svs.filter { $0 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在此输入图片描述


我看到你在MasterViewController中添加了UISearchControllerDelegate,但是你也添加了UISearchBarDelegate吗? - Dris
已完成。谢谢 :) - g0ld2k
我现在两个都有了。你可以在最新的推送中看到。 - g0ld2k
我将 navigationItem.searchController = sc 更改为 navigationItem.titleView = sc.searchBar,这对我有效。 - Dris
这是有效的,但在紧凑的导航栏布局中会丢失标题,并将标题放置在大型导航栏布局中搜索栏下方。 - g0ld2k
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接