Swift修改UISearchBar中选中ScopeBar按钮的背景色
修改UISearchBar选中状态下ScopeBar按钮的背景色
我刚接触Swift,目前尝试修改UISearchBar中选中状态下ScopeBar按钮的背景色,当前该按钮背景色为白色,想要更换为其他颜色。以下是我用于添加并配置UISearchBar的代码:
private let searchController = UISearchController(searchResultsController: nil) searchController.searchBar.delegate = self searchController.searchBar.backgroundColor = UIColor(named: "TopicColor") navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false navigationController?.navigationBar.prefersLargeTitles = true let appearance = UINavigationBarAppearance(idiom: .phone) appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] appearance.titleTextAttributes = [.foregroundColor: UIColor.white] appearance.backgroundColor = UIColor(named: "TopicColor") navigationItem.standardAppearance = appearance navigationItem.scrollEdgeAppearance = appearance searchController.obscuresBackgroundDuringPresentation = false searchController.searchBar.placeholder = "Search" searchController.searchBar.showsScopeBar = true searchController.searchBar.scopeButtonTitles = ["Movie", "Series"] UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor(named: "Color-1") let placeholderAppearance = UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]) placeholderAppearance.textColor = UIColor.white let searchTextAppearance = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) searchTextAppearance.textColor = UIColor.white searchController.searchBar.tintColor = UIColor.white searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "Color-1")!], for: UIControl.State.normal)
解决方案
方法1:利用UIAppearance统一设置(iOS 13+)
ScopeBar本质是UISegmentedControl,可以通过外观代理针对嵌套在UISearchBar里的分段控件设置选中背景色:
// 工具方法:用颜色生成纯色图片 extension UIImage { static func imageWithColor(color: UIColor) -> UIImage? { let rect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context?.setFillColor(color.cgColor) context?.fill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } // 在配置SearchBar的代码后添加以下设置 if let selectedBgImage = UIImage.imageWithColor(color: UIColor(named: "YourTargetColor")!) { // 设置选中状态的背景图 UISegmentedControl.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setBackgroundImage(selectedBgImage, for: .selected, barMetrics: .default) // 可选:设置分割线颜色,避免出现默认白色分割线 UISegmentedControl.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setDividerImage(selectedBgImage, forLeftSegmentState: .normal, rightSegmentState: .selected, barMetrics: .default) UISegmentedControl.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setDividerImage(selectedBgImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default) }
方法2:遍历子视图定位控件(兼容旧版本)
如果需要兼容iOS 13以下系统,可以通过递归遍历SearchBar的子视图找到对应的UISegmentedControl:
// 递归查找ScopeBar对应的分段控件 func findScopeSegmentedControl(in view: UIView) -> UISegmentedControl? { for subview in view.subviews { if let segmentedControl = subview as? UISegmentedControl { return segmentedControl } else if let found = findScopeSegmentedControl(in: subview) { return found } } return nil } // 在viewDidAppear或者searchBar回调中调用(确保ScopeBar已加载) override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if let scopeSegment = findScopeSegmentedControl(in: searchController.searchBar) { let targetColor = UIColor(named: "YourTargetColor")! let selectedBgImage = UIImage.imageWithColor(color: targetColor) scopeSegment.setBackgroundImage(selectedBgImage, for: .selected, barMetrics: .default) // 可选:设置分割线 scopeSegment.setDividerImage(selectedBgImage, forLeftSegmentState: .normal, rightSegmentState: .selected, barMetrics: .default) scopeSegment.setDividerImage(selectedBgImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default) } }
补充:调整选中状态的文字颜色
如果需要同步修改选中按钮的文字颜色,添加以下代码:
searchController.searchBar.setScopeBarButtonTitleTextAttributes([ NSAttributedString.Key.foregroundColor: UIColor.white // 替换为你需要的文字颜色 ], for: .selected)
内容的提问来源于stack exchange,提问作者Kolykhalov Stas
相关产品推荐
相关产品推荐

