如何在 iOS 11 中更改 UISearchController SearchBar 颜色

How to change UISearchController SearchBar Colour in iOS 11

我想自定义搜索栏的背景颜色和文本颜色,我已尝试使用以下代码进行自定义,但没有成功。

extension UISearchBar {

    private func getViewElement<T>(type: T.Type) -> T? {
        let svs = subviews.flatMap { [=11=].subviews }
        guard let element = (svs.filter { [=11=] is T }).first as? T else { return nil }
        return element
    }

    func getSearchBarTextField() -> UITextField? {
        return getViewElement(type: UITextField.self)
    }

    func getSearchBarButton() -> UIButton? {
        return getViewElement(type: UIButton.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 = 18.0
                if let backgroundview = textField.subviews.first {
                    backgroundview.layer.cornerRadius = 18.0;
                    backgroundview.clipsToBounds = true;
                }
            case .prominent, .default:
                textField.backgroundColor = color
            }
        }
    }

    func setPlaceholderTextColor(color: UIColor) {
        if let textField = getSearchBarTextField() {
            textField.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes: [NSAttributedStringKey.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)
            }else{
                //button.setImage(#imageLiteral(resourceName: "icon-hotel"), for: .normal)
            }
        }
        if let btn = getSearchBarButton() {
            let button = btn.value(forKey: "_clearButton") as! UIButton
            if let image = button.imageView?.image {
                button.setImage(image.transform(withNewColor: color), for: .normal)
            }else{
                //button.setImage(#imageLiteral(resourceName: "icon-hotel"), for: .normal)
            }
        }

    }

    func setSearchImageColor(color: UIColor) {
        if let imageView = getSearchBarTextField()?.leftView as? UIImageView {
            imageView.image = imageView.image?.transform(withNewColor: color)
        }
    }
}

extension UIImage {

    func transform(withNewColor color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)

        let context = UIGraphicsGetCurrentContext()!
        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(.normal)

        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        context.clip(to: rect, mask: cgImage!)

        color.setFill()
        context.fill(rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
}

只有当我只使用 UISearchbar 时它才工作,但当它是 UISearchController 时不工作。

请查看下图以获得更多理解。我需要 text/placeholder 色白色。 clear/search 图片颜色为白色。

我已经使用了下面的代码。

self.title = "Skills"
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.isNavigationBarHidden = false
        self.navigationController?.navigationBar.barTintColor = UIColor.hexString("6EC280") //light green

        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

        //SEARCH BAR CUSTOMIZATION
        let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 30.0, height: 50.0))
        btnBack.setImage(#imageLiteral(resourceName: "chevron-back"), for: .normal)
        btnBack.addTarget(self, action: #selector(btnBack(_:)), for: .touchUpInside)
        let BarItem = UIBarButtonItem(customView: btnBack)
        navigationItem.leftBarButtonItem = BarItem

        UISearchBar.appearance().tintColor = UIColor.white
        UINavigationBar.appearance().tintColor = UIColor.white

        searchBarController.searchResultsUpdater = self
        searchBarController.obscuresBackgroundDuringPresentation = false
        navigationItem.searchController = searchBarController
        navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true

        searchBarController.searchBar.placeholder = "Search"
        searchBarController.searchBar.setTextColor(color: .white)
        searchBarController.searchBar.setPlaceholderTextColor(color: .white)
        searchBarController.searchBar.setSearchImageColor(color: .white)
        searchBarController.searchBar.setTextFieldColor(color: UIColor.hexString("549C64")) //light green
        searchBarController.searchBar.delegate = self

我已将代码放入主队列及其工作。

DispatchQueue.main.async {
            self.searchBarController.searchBar.placeholder = "Search"
            self.searchBarController.searchBar.setTextColor(color: .white)
            self.searchBarController.searchBar.setPlaceholderTextColor(color: .white)
            self.searchBarController.searchBar.setSearchImageColor(color: .white)
            self.searchBarController.searchBar.setTextFieldColor(color: UIColor.clear) //light green
            self.searchBarController.searchBar.delegate = self
        }

奇怪:)

在您的 AppDelegate 中,在 didFinishLaunchingWithOptions 函数中添加以下代码行:

UISearchBar.appearance().tintColor = .green  // Add your color
UISearchBar.appearance().backgroundColor = .white  // Add your color