iOS 11 UISearchBar 背景颜色
iOS 11 UISearchBar background color
我知道这个问题已经被问过很多次了,很多次。然而,正如 Apple 做得最好的那样,随着 iOS 11 的发布,他们似乎对 UISearchBar
进行了看似不必要的更改,特别是它的视图层次结构。
此外,搜索栏的 "text field" 在搜索栏的子视图中不再可用,导致 所有 以前的解决方案 "access"并更改文本字段的背景颜色,或与此相关的文本字段的任何 属性。
- 有谁知道如何实际调整iOS11中搜索栏的背景颜色?
仅供参考:
我特别是在谈论文本背后的颜色...从 11 开始默认为白色,除非您将搜索栏样式指定为最小。
更新 1:
自从我发布这个问题后,我仍然没有找到有效的或真正的任何真正的解决方案来解决这个问题。我似乎最接近的是深入研究实例属性的外观
[[UISearchBar class] appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)]
个 UISearchBar
。通过以下方法处理找到的 UITextField
:
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchTextField;
for (UIView *subview in view.subviews) {
searchTextField = [self searchViewForTextFieldBg:subview];
if (searchTextField) {
break;
}
}
return searchTextField;
您可以开始绘制 新 背景视图以放置在视图后面。然而,我发现的问题太乏味而无法进一步研究,即绘制具有正确框架/边界的视图以准确模仿原始背景。
希望有人能找到实际解决这个问题的方法。漂亮的苹果小姐...
此 Swift 代码更改文本字段的背景颜色:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
这是结果
我想你可能正在寻找这个,对吧?但我在 Swift :(
@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
textfield.backgroundColor = UIColor.yellow
}
这是结果:
let searchBar = UISearchBar(frame: CGRect())
let searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
let searchBarBackground: UIView? = searchBar.value(forKey: "background") as? UIView
// searchBarBackground?.removeFromSuperview()
if searchField != nil {
var frame = searchField?.frame
frame?.size.height = 30
searchField?.frame = frame!
searchField?.backgroundColor = .yellow
}
searchBar.barTintColor = .red
searchBar.delegate = self
searchBar.backgroundColor = .green
运行时视图层次结构
如果我们使用上面的代码为 UISearchBar 设置背景颜色,我们将看到彩色子视图如下图(点击链接查看)。

backgroundColor
用于 UISearchBar 子视图的超级视图
我们可以在Object inspector.
中看到绿色视图的Class名称是UISearchBar
因此,如果我们使用 searchBar.backgroundColor = .green
,我们将设置 Superview 的背景色为绿色。因此,UISearchBar 实例 属性 backgroundColor
将设置超级视图的背景颜色。
Superview of UISearchBar
barTintColor
用于 UISearchBarBackground
我们可以在Object inspector.
中看到Class红色视图的名字是UISearchBarBackground
但是,没有直接访问视图的方法,我们可以使用KVC searchBar.value(forKey: "background") as? UIView
尝试获取searchBarBackground。
如果我们使用searchBar.barTintColor = .red
,我们会将UISearchBarBackground 视图的backgroundColor 设置为红色。 为了去除 tint bar 层上的两个黑色边框,我们必须从 superview 中去除背景。
barTintColor of UISearchBar
searchField?.backgroundColor
用于 UITextField
我们可以在对象检查器中看到Class黄色视图的名称是_UISearchBarSearchFieldBackgroundView(UISearchBarTextField 的子视图)。
没有直接访问 searchField 的方法,与 searchBarBackground 相同。我们也可以使用KVCsearchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
尝试得到searchField.
如果我们使用searchField?.backgroundColor = .yellow
,我们会将 UITextField 的背景颜色设置为黄色。因此,如果我们要设置文本字段背景颜色,我们必须首先使用 KVC
访问 searchField
UITextField of UISearchBar
Swift 4-5
searchController.searchBar.barTintColor = .white
比Swift5.
简单多了
searchBar.barTintColor = .black
searchBar.searchTextField.backgroundColor = .white
我知道这个问题已经被问过很多次了,很多次。然而,正如 Apple 做得最好的那样,随着 iOS 11 的发布,他们似乎对 UISearchBar
进行了看似不必要的更改,特别是它的视图层次结构。
此外,搜索栏的 "text field" 在搜索栏的子视图中不再可用,导致 所有 以前的解决方案 "access"并更改文本字段的背景颜色,或与此相关的文本字段的任何 属性。
- 有谁知道如何实际调整iOS11中搜索栏的背景颜色?
仅供参考: 我特别是在谈论文本背后的颜色...从 11 开始默认为白色,除非您将搜索栏样式指定为最小。
更新 1:
自从我发布这个问题后,我仍然没有找到有效的或真正的任何真正的解决方案来解决这个问题。我似乎最接近的是深入研究实例属性的外观
[[UISearchBar class] appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)]
个 UISearchBar
。通过以下方法处理找到的 UITextField
:
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchTextField;
for (UIView *subview in view.subviews) {
searchTextField = [self searchViewForTextFieldBg:subview];
if (searchTextField) {
break;
}
}
return searchTextField;
您可以开始绘制 新 背景视图以放置在视图后面。然而,我发现的问题太乏味而无法进一步研究,即绘制具有正确框架/边界的视图以准确模仿原始背景。
希望有人能找到实际解决这个问题的方法。漂亮的苹果小姐...
此 Swift 代码更改文本字段的背景颜色:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
这是结果
我想你可能正在寻找这个,对吧?但我在 Swift :(
@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
textfield.backgroundColor = UIColor.yellow
}
这是结果:
let searchBar = UISearchBar(frame: CGRect())
let searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
let searchBarBackground: UIView? = searchBar.value(forKey: "background") as? UIView
// searchBarBackground?.removeFromSuperview()
if searchField != nil {
var frame = searchField?.frame
frame?.size.height = 30
searchField?.frame = frame!
searchField?.backgroundColor = .yellow
}
searchBar.barTintColor = .red
searchBar.delegate = self
searchBar.backgroundColor = .green
运行时视图层次结构
如果我们使用上面的代码为 UISearchBar 设置背景颜色,我们将看到彩色子视图如下图(点击链接查看)。 
backgroundColor
用于 UISearchBar 子视图的超级视图
我们可以在Object inspector.
中看到绿色视图的Class名称是UISearchBar因此,如果我们使用 searchBar.backgroundColor = .green
,我们将设置 Superview 的背景色为绿色。因此,UISearchBar 实例 属性 backgroundColor
将设置超级视图的背景颜色。
Superview of UISearchBar
barTintColor
用于 UISearchBarBackground
我们可以在Object inspector.
中看到Class红色视图的名字是UISearchBarBackground但是,没有直接访问视图的方法,我们可以使用KVC searchBar.value(forKey: "background") as? UIView
尝试获取searchBarBackground。
如果我们使用searchBar.barTintColor = .red
,我们会将UISearchBarBackground 视图的backgroundColor 设置为红色。 为了去除 tint bar 层上的两个黑色边框,我们必须从 superview 中去除背景。
barTintColor of UISearchBar
searchField?.backgroundColor
用于 UITextField
我们可以在对象检查器中看到Class黄色视图的名称是_UISearchBarSearchFieldBackgroundView(UISearchBarTextField 的子视图)。
没有直接访问 searchField 的方法,与 searchBarBackground 相同。我们也可以使用KVCsearchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
尝试得到searchField.
如果我们使用searchField?.backgroundColor = .yellow
,我们会将 UITextField 的背景颜色设置为黄色。因此,如果我们要设置文本字段背景颜色,我们必须首先使用 KVC
UITextField of UISearchBar
Swift 4-5
searchController.searchBar.barTintColor = .white
比Swift5.
简单多了 searchBar.barTintColor = .black
searchBar.searchTextField.backgroundColor = .white