You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 11中如何自定义UISearchController的UISearchBar背景与文本颜色?

嘿,我来帮你搞定UISearchBar自定义背景色和文本色的问题!你之前的代码没达到预期效果,大概率是因为iOS版本更新后,UISearchBar的内部视图结构发生了变化,原来手动遍历子视图找UITextField的方式已经不再靠谱啦。

解决UISearchBar自定义样式问题

先说说你现有代码的问题

在iOS 13及以后的版本中,Apple已经给UISearchBar直接提供了searchTextField属性,不需要再通过遍历子视图的方式去查找输入框。旧的遍历方法不仅繁琐,还容易因为系统视图结构的调整而失效。

下面给你两种稳定可靠的实现方案:

方案一:使用系统原生属性(推荐,适配最新iOS)

这种方式最简洁也最稳定,完全利用系统提供的API:

extension UISearchBar {
    /// 设置搜索栏整体背景色
    func setCustomBackgroundColor(_ color: UIColor) {
        backgroundColor = color
        // 如果需要给输入框单独设置背景色,可以添加这一行
        searchTextField.backgroundColor = .white
    }
    
    /// 设置文本、占位符和光标颜色
    func setCustomTextStyles(textColor: UIColor, placeholderColor: UIColor) {
        // 设置输入文本颜色
        searchTextField.textColor = textColor
        // 设置占位符颜色
        if let placeholder = searchTextField.placeholder {
            searchTextField.attributedPlaceholder = NSAttributedString(
                string: placeholder,
                attributes: [.foregroundColor: placeholderColor]
            )
        }
        // 设置光标颜色(可选)
        searchTextField.tintColor = textColor
    }
}

使用示例:

let mySearchBar = UISearchBar()
mySearchBar.setCustomBackgroundColor(.lightGray)
mySearchBar.setCustomTextStyles(textColor: .black, placeholderColor: .darkGray)

方案二:兼容旧iOS版本的遍历方式(支持iOS 12及以下)

如果你的项目需要适配更早的iOS版本,可以优化遍历逻辑,用递归方式确保找到输入框:

extension UISearchBar {
    private func findInnerTextField() -> UITextField? {
        // 递归遍历所有子视图查找UITextField
        func traverseSubviews(_ view: UIView) -> UITextField? {
            for subview in view.subviews {
                if let textField = subview as? UITextField {
                    return textField
                }
                if let foundTextField = traverseSubviews(subview) {
                    return foundTextField
                }
            }
            return nil
        }
        return traverseSubviews(self)
    }
    
    func setCustomBackgroundColor(_ color: UIColor) {
        backgroundColor = color
        findInnerTextField()?.backgroundColor = .white
    }
    
    func setCustomTextStyles(textColor: UIColor, placeholderColor: UIColor) {
        guard let textField = findInnerTextField() else { return }
        textField.textColor = textColor
        if let placeholder = textField.placeholder {
            textField.attributedPlaceholder = NSAttributedString(
                string: placeholder,
                attributes: [.foregroundColor: placeholderColor]
            )
        }
        textField.tintColor = textColor
    }
}

额外小技巧

如果需要修改搜索栏右侧的取消按钮颜色,直接设置tintColor即可:

mySearchBar.tintColor = .blue // 取消按钮的文字和图标都会变成这个颜色

注意:建议在viewDidLayoutSubviews方法中调用这些自定义方法,确保UISearchBar已经完成布局,避免出现找不到视图的情况。

内容的提问来源于stack exchange,提问作者Ilesh Panchal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:17:34