UISearchController iOS 11自定义
在iOS 11之前,我一直使用下面的代码来自定义UISearchController
search栏的外观:
var searchController = UISearchController(searchResultsController: nil) searchController.searchBar.setDefaultSearchBar() searchController.searchResultsUpdater = self if #available(iOS 11.0, *) { navigationItem.searchController = searchController } else { tableView.tableHeaderView = searchController.searchBar } extension UISearchBar { func setDefaultSearchBar() { self.tintColor = UIColor.blue self.searchBarStyle = .minimal self.backgroundImage = UIImage(color: UIColor.clear) let searchBarTextField = self.value(forKey: "searchField") as! UITextField searchBarTextField.textColor = UIColor.white searchBarTextField.tintColor = UIColor.blue searchBarTextField = .dark } }
但是,更新到iOS 11并添加UISearchController
作为navigationItem
一部分, 任何自定义(除了键盘外观certificate我的代码已被调用)未能显示。 这是一个错误,或者我有不正确的实施?
编辑:附加的设备运行iOS 10(第一)和iOS 11的两个屏幕截图
编辑2:迄今为止关于这个问题的注意力集中在UISearchBar
的文本颜色上。 我看到的不仅仅是背景颜色,色调,search指示器和清除button的颜色等, 所有这些都不能在运行iOS 11的设备上进行定制,尽pipe在iOS 10中完全可以这样做。
我刚刚发现如何设置它们(在Brandon和Krunal的帮助下,谢谢!)
“取消”文本:
searchController.searchBar.tintColor = .white
search图标:
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)
search文本:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
占位符:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
白色背景:
if #available(iOS 11.0, *) { let sc = UISearchController(searchResultsController: nil) sc.delegate = self let scb = sc.searchBar scb.tintColor = UIColor.white scb.barTintColor = UIColor.white if let textfield = scb.value(forKey: "searchField") as? UITextField { textfield.textColor = UIColor.blue if let backgroundview = textfield.subviews.first { // Background color backgroundview.backgroundColor = UIColor.white // Rounded corner backgroundview.layer.cornerRadius = 10; backgroundview.clipsToBounds = true; } } if let navigationbar = self.navigationController?.navigationBar { navigationbar.barTintColor = UIColor.blue } navigationItem.searchController = sc navigationItem.hidesSearchBarWhenScrolling = false }
从这里采取。
将调用setDefaultSearchBar
移动到viewDidAppear
应该解决这个问题。
尝试设置search栏的栏样式。
searchController.searchBar.barStyle = UIBarStyleBlack;
要将input到search栏中的文本正确设置为白色(使用暗场颜色时):
searchController.searchBar.barStyle = .black
设置文本框的背景颜色
if #available(iOS 11.0, *) { if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField { if let backgroundview = textfield.subviews.first { // Background color backgroundview.backgroundColor = UIColor.white // Rounded corner backgroundview.layer.cornerRadius = 10; backgroundview.clipsToBounds = true; } } }
但是使用类似的东西
textfield.textColor = UIColor.blue
在上面似乎没有工作。
您必须访问UISearchBar中的UITextField。 你可以通过使用
let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField textFieldInsideSearchBar?.textColor = yourcolor
要么
您需要findUISearchBar
的底层UITextField
并更改其文本颜色。
注意,这只有在search控制器要呈现( UISearchControllerDelegate.willPresentSearchController
)或呈现时才有效果。
class ViewController : UIViewController { override func viewDidLoad() { super.viewDidLoad() // setup your search controller... // set search controller's delegate navigationItem.searchController?.delegate = self } } extension ViewController : UISearchControllerDelegate { func willPresentSearchController(_ searchController: UISearchController) { // update text color searchController.searchBar.textField?.textColor = .white } } extension UISearchBar { var textField: UITextField? { for subview in subviews.first?.subviews ?? [] { if let textField = subview as? UITextField { return textField } } return nil } }
如果您需要更改searchBar中textField的背景颜色,请参阅我的答案: https ://stackoverflow.com/a/46315974/1109892
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]