我想把一个搜索栏在新的导航栏与新的iOS 11大标题。但是,搜索栏的颜色是由iOS自动应用的,我无法更改它。
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
搜索栏是深蓝色背景,但我只想把它改成白色。
设置背景色的结果如下:
navigationItem.searchController?.searchBar.backgroundColor = UIColor.white
我也在搜索栏上尝试过setScopeBarButtonBackgroundImage
和setBackgroundImage
,但是一切看起来都很奇怪。
此外,当我通过点击搜索栏触发搜索时,它切换到右侧的cancel按钮的模式。(德语中的“Abbrechen”)
而"Abbrechen“文本的颜色也不能改变。(也需要白色)
任何帮助都是非常感谢的。
编辑:根据请求,在这里显示导航条样式的代码:
self.navigationBar.tintColor = UIColor.myWhite
self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarTitle()]
self.navigationBar.barTintColor = UIColor.myTint
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarLargeTitle()]
}
当前结果:我已经使用Krunals的想法来设置搜索栏背景的颜色,但是圆角会丢失。重新设置圆角后,搜索栏的动画似乎被打破了。
所以仍然没有令人满意的解决办法。似乎搜索栏嵌入到iOS 11中的导航栏中是不可能自定义的。同时,只要更改占位符文本的颜色就足够了,但这似乎是不可能的。(我尝试过StackOverflow的多种方法-不起作用)
发布于 2017-09-01 12:06:44
这就是你想要的..。
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
}
结果:
圆角:
带有圆角的动画也工作得很好。
发布于 2017-10-08 11:57:17
我在AppDelegate中用这段代码更改了文本字段的背景。
Swift 4
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
这就是结果
发布于 2017-09-01 10:50:43
这应该对你有用
func addSearchbar(){
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
let scb = sc.searchBar
scb.tintColor = UIColor.white
if let navigationbar = self.navigationController?.navigationBar {
//navigationbar.tintColor = UIColor.green
//navigationbar.backgroundColor = UIColor.yellow
navigationbar.barTintColor = UIColor.blue
}
navigationController?.navigationBar.tintColor = UIColor.green
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
}
结果
https://stackoverflow.com/questions/45997996
复制相似问题