【问题标题】:UISearchBar color in NavigationBarNavigationBar 中的 UISearchBar 颜色
【发布时间】:2019-07-19 20:20:56
【问题描述】:

我觉得我正在服用疯狂的药丸。我想更改 UISearchBar 中文本字段的颜色,该 UISearchBar 托管在 UINavigationBar 中。我遇到的问题是 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

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

【问题讨论】:

    标签: ios uinavigationbar uisearchbar ios12 ios13


    【解决方案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

    【讨论】:

    • 同样的问题,我添加了扩展并设置了颜色:scb.setTextFieldColor(color: UIColor.green) scb.setTextColor(color: UIColor.red) 结果仍然是带有导航栏色调的搜索栏。
    • 您是否将 UISearchBarDelegate 添加到您的班级? class ViewController: UIViewController, UISearchBarDelegate
    • 是的,并将委托分配给自己,但我在委托方法中看不到任何与 UI 相关的内容。
    • 请更新 git
    • 我看到您在 MasterViewController 中添加了 UISearchControllerDelegate,但您是否也添加了 UISearchBarDelegate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多