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

如何自定义UISearchBar内UITextField的背景色及其他属性?

解决UISearchBar内部UITextField背景色被默认tertiarySystemFillColor覆盖的问题

问题根源

UISearchBar的searchTextField默认带有一个内置的backgroundView,这个视图的默认颜色就是tertiarySystemFillColor,直接设置textField.backgroundColor会被这个内置视图覆盖,导致自定义颜色不生效。另外你代码中同时通过官方searchTextField和KVC方式获取文本框,存在重复设置的冲突,也会影响样式生效。

解决方案步骤

  • 移除/修改默认背景视图:直接操作文本框的backgroundView,要么修改它的颜色,要么直接移除
  • 使用官方API替代KVC:iOS 13+提供了官方的searchTextField属性,无需依赖私有KVC方式,兼容性更好
  • 统一配置入口:避免同时操作多个文本框实例,确保样式设置的一致性

修正后的完整代码

func configureMarkerSearchBar(with placeholder: String) {
    backgroundColor = .clear
    tintColor = .clear
    
    // 直接使用官方提供的searchTextField属性
    let textField = searchTextField
    
    // 处理默认背景视图,解决tertiarySystemFillColor覆盖问题
    textField.backgroundView?.backgroundColor = .clear
    // 若不需要默认背景视图,也可以直接移除:textField.backgroundView = nil
    
    // 配置文本框圆角与裁剪
    textField.layer.cornerRadius = CGFloat(SearchBarConstants.searchBarCornerRadius)
    textField.layer.masksToBounds = true
    textField.clipsToBounds = true
    
    // 配置占位符与字体
    let attributedString = NSAttributedString(string: placeholder,
                                              attributes: [NSAttributedString.Key.foregroundColor:
                                                             SearchBarConstants.searchBarTextInputColor])
    textField.attributedPlaceholder = attributedString
    textField.font = UIFont.regularDMSans(of: SearchBarConstants.textFieldFontSize)
    
    // 配置文本框布局与基础样式
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.backgroundColor = .clear // 此时设置会正常生效
    textField.tintColor = .clear
    
    NSLayoutConstraint.activate([
        textField.heightAnchor.constraint(equalToConstant: 46),
        textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
        textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
        textField.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
    ])
    
    // 配置自定义搜索图标
    if let searchIcon = UIImage(named: SearchBarConstants.iconSearch)?.withRenderingMode(.alwaysTemplate) {
        let imageView = UIImageView(image: searchIcon)
        imageView.tintColor = SearchBarConstants.searchBarTextInputColor
        
        let paddingView = UIView(frame: CGRect(x: 0, y: 0,
                                               width: SearchBarConstants.searchIconOffset + searchIcon.size.width,
                                               height: searchIcon.size.height))
        imageView.frame = CGRect(x: SearchBarConstants.searchIconOffset, y: 0,
                                 width: searchIcon.size.width,
                                 height: searchIcon.size.height)
        paddingView.addSubview(imageView)
        textField.leftView = paddingView
        textField.leftViewMode = .always
    }
    
    sizeToFit()
}

额外注意事项

  • 若需要设置自定义背景色,将代码中的.clear替换为你需要的颜色即可,比如UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.0)
  • 确保在UISearchBar初始化完成后调用该配置方法,比如在viewDidLoad或自定义初始化方法中
  • 如需兼容iOS 12及以下版本,可添加版本判断保留KVC兼容逻辑:
if #available(iOS 13.0, *) {
    let textField = searchTextField
    // 配置代码
} else {
    if let textField = value(forKey: SearchBarConstants.searchFieldKey) as? UITextField {
        // iOS 12及以下兼容配置
        textField.backgroundView?.backgroundColor = .clear
        // 其他样式设置
    }
}

内容的提问来源于stack exchange,提问作者ezgi özkan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 22:43:27