iOS 11自定义导航栏中的搜索栏 [英] iOS 11 customise search bar in navigation bar
问题描述
我想在iOS 11搜索栏中嵌入导航栏时更改文本和图标的颜色。所以占位符文本,搜索文本和搜索图标。
if #available(iOS 11.0,*){
navigationController?.navigationBar.prefersLargeTitles = false
let searchController = UISearchController(searchResultsController:nil)
navigationItem .searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchBar.placeholder =Suchen
searchController.searchBar.tintColor = .white
}
正如您在图像中看到的那样,深蓝色背景上的文字是灰色的,看起来很难看。我希望文字和图标至少是白色的。 (更改蓝色背景颜色也不是很好,请参阅
占位符:
UITextField.appearance(whenContainedInInstancesOf:[UISearchBar.self]) .attributedPlaceholder = NSAttributedString(string:placeholder,attributes:[NSAttributedStringKey.foregroundColor:UIColor.white])
白色背景:
如果#available(iOS 11.0,*){
let sc = UISearchController( searchResultsController:nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
如果让textfield = scb.value(forKey:searchField)为? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
//背景颜色
backgroundview.backgroundColor = UIColor.white
//圆角
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
如果让navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem .searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
取自这里。
I want to change the color of the text and icon in the iOS 11 searchbar when it is embedded in the navigation bar. So placeholder text, search text and search icon.
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = false
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchBar.placeholder = "Suchen"
searchController.searchBar.tintColor = .white
}
As you can see in the image, the text is grey on a deep blue background, which looks ugly. I want to text and icon to be at least white. (changing the blue background color also does not work really good, see my other question)
The only thing which works is changing the color of the blinking cursor and the "cancel" button, which is done with the .tintColor property.
Solutions which seems to work in iOS 10 and below seem not work anymore in iOS 11, so please post only solutions which you know working in iOS 11. Thanks.
Maybe I miss the point about this "automatic styling" in iOS 11. Any help is appreciated.
I just found out how to set also the rest of them: (with some help of Brandon, thanks!)
The "Cancel" text:
searchController.searchBar.tintColor = .white
The search icon:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
The clear icon:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
The search text:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
Thanks for the help @Brandon!
The placeholder:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
The white background:
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
}
Taken from here.
这篇关于iOS 11自定义导航栏中的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!