我刚刚发现了如何设置剩余部分:(在Brandon的帮助下,谢谢!)
“取消”文本:
searchController.searchBar.tintColor = .white
搜索图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
清除图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
搜索文本:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
感谢 @Brandon 的帮助!
![输入图像描述](https://istack.dev59.com/QrB9B.webp)
占位符:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
![这里输入图片描述](https://istack.dev59.com/5qdvs.webp)
白色背景:
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
let searchBar = searchController.searchBar
searchBar.tintColor = UIColor.white
searchBar.barTintColor = UIColor.white
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
![这里输入图片描述](https://istack.dev59.com/vPbx7.webp)
来源:这里。
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .white
吗? - MwcsMac