无法更改搜索栏背景颜色 [英] Cannot change search bar background color
本文介绍了无法更改搜索栏背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个搜索栏:
let searchBar:UISearchBar = UISearchBar(frame: CGRectMake((searchView.frame.width - UIScreen.mainScreen().bounds.width / 1.6) / 2, 0, UIScreen.mainScreen().bounds.width / 1.6, 24))
我想更改文本输入部分背景颜色。为此,我试过:
and I want to change text input part background color. For this I've tried:
searchBar.barTintColor = UIColor(red: 0/255, green: 74/255, blue: 103/255, alpha: 1)
searchBar.backgroundColor = UIColor.redColor()
但这两种变体都不起作用。如何更改我的UISearchBar textInput部分的背景颜色以及我做错了什么?
but these both variants do not work. How can I change background color of my UISearchBar textInput part and what I did wrong?
推荐答案
如果您只想更改它您的ViewController并不希望其他任何地方生效,然后使用
If you only want to change it in your ViewController and don't want anywhere else to effect then use
for view in searchBar.subviews {
for subview in view.subviews {
if subview .isKindOfClass(UITextField) {
let textField: UITextField = subview as! UITextField
textField.backgroundColor = UIColor.redColor()
}
}
}
但如果您希望在整个应用程序中进行更改并定位到iOS 9.0或更高版本,则应使用appearanceWhenContainedInInstancesOfClasses,如
But if you want it to be change in whole app and targeting the iOS 9.0 or later then should be using appearanceWhenContainedInInstancesOfClasses like
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).backgroundColor = UIColor.redColor()
这篇关于无法更改搜索栏背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文