UISearchBar 不会删除 UISearchBarBackground
UISearchBar wont remove UISearchBarBackground
我已尽一切努力让它工作。我像这样设置了自定义 class。
override func layoutSubviews() {
super.layoutSubviews()
clearBackgroundColor() // function in the question
}
private func clearBackgroundColor() {
guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }
for view in self.subviews {
for subview in view.subviews {
if subview.isKind(of: UISearchBarBackground) {
subview.alpha = 0
}
}
}
}
我将 backgroundColor、barTintColor 设置为 .clear。风格极简。我疯了。我设置断点以确保我们找到搜索栏背景。我也试过 subview.removeFromSuperview() 。没有什么。我想我疯了。我错过了什么吗?
这是在 iOS 10 上使用的故事板。任何帮助将不胜感激。
前一段时间我不得不在客户的应用程序中执行此操作。以下是对我有用的方法:
我有一个 UISearchBar
子类:
@property (nonatomic, strong) UITextField* textField;
我从 init
调用了以下内容:
self.textField = [self findViewOfClass:[UITextField class] inView:self];
self.translucent = NO;
self.barTintColor = ...;
self.textField.backgroundColor = ...;
- (id)findViewOfClass:(Class)class inView:(UIView*)view
{
if ([view isKindOfClass:class])
{
return view;
}
else
{
for (UIView* subview in view.subviews)
{
id foundView = [self findViewOfClass:class inView:subview];
if (foundView != nil)
{
return foundView;
}
}
}
return nil;
}
关键部分是找到 UITextField
。 (我做了类似的事情来允许我自定义取消按钮的样式。)我依稀记得确实需要禁用 translucent
;容易尝试。
应该是这样。让我知道这是否适合你。
我只有Obj-C代码,但是这个很容易转换。
我终于忽略了所有关于这个主题的帖子的先前答案,并做了我自己的 Debug View Hierarchy。我发现了一个用作背景的 ImageView,我猜它现在称为“_UISearchBarSearchFieldBackgroundView”。这帮助我找到了一个至少可以解决 iOS 9+ 问题的函数。
searchBar.setSearchFieldBackgroundImage(UIImage(), for: .normal)
需要注意的一点是,这不是解决此问题的唯一方法。但是,我使用它是因为它不需要循环,并且因为图像是空的,所以永远不会添加额外的视图,从而提供与其他方法相同的最终结果。
需要注意的一件事是,这可能只适用于 iOS 9+。因此,您的里程数可能会有所不同。我使用 iOS 10 进行了测试,部署目标为 9.3。
我已尽一切努力让它工作。我像这样设置了自定义 class。
override func layoutSubviews() {
super.layoutSubviews()
clearBackgroundColor() // function in the question
}
private func clearBackgroundColor() {
guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }
for view in self.subviews {
for subview in view.subviews {
if subview.isKind(of: UISearchBarBackground) {
subview.alpha = 0
}
}
}
}
我将 backgroundColor、barTintColor 设置为 .clear。风格极简。我疯了。我设置断点以确保我们找到搜索栏背景。我也试过 subview.removeFromSuperview() 。没有什么。我想我疯了。我错过了什么吗?
这是在 iOS 10 上使用的故事板。任何帮助将不胜感激。
前一段时间我不得不在客户的应用程序中执行此操作。以下是对我有用的方法:
我有一个 UISearchBar
子类:
@property (nonatomic, strong) UITextField* textField;
我从 init
调用了以下内容:
self.textField = [self findViewOfClass:[UITextField class] inView:self];
self.translucent = NO;
self.barTintColor = ...;
self.textField.backgroundColor = ...;
- (id)findViewOfClass:(Class)class inView:(UIView*)view
{
if ([view isKindOfClass:class])
{
return view;
}
else
{
for (UIView* subview in view.subviews)
{
id foundView = [self findViewOfClass:class inView:subview];
if (foundView != nil)
{
return foundView;
}
}
}
return nil;
}
关键部分是找到 UITextField
。 (我做了类似的事情来允许我自定义取消按钮的样式。)我依稀记得确实需要禁用 translucent
;容易尝试。
应该是这样。让我知道这是否适合你。
我只有Obj-C代码,但是这个很容易转换。
我终于忽略了所有关于这个主题的帖子的先前答案,并做了我自己的 Debug View Hierarchy。我发现了一个用作背景的 ImageView,我猜它现在称为“_UISearchBarSearchFieldBackgroundView”。这帮助我找到了一个至少可以解决 iOS 9+ 问题的函数。
searchBar.setSearchFieldBackgroundImage(UIImage(), for: .normal)
需要注意的一点是,这不是解决此问题的唯一方法。但是,我使用它是因为它不需要循环,并且因为图像是空的,所以永远不会添加额外的视图,从而提供与其他方法相同的最终结果。
需要注意的一件事是,这可能只适用于 iOS 9+。因此,您的里程数可能会有所不同。我使用 iOS 10 进行了测试,部署目标为 9.3。