使UISearchBar背景清晰

问题描述 投票:22回答:13

我无法清除搜索栏,我试图通过将其背景颜色设置为透明来使其变得清晰,并且我也将一幅图像放置在搜索栏下

我也已经明确了搜索栏的背景

 for (UIView *subview in self.searchBarHome.subviews) {
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
         [subview removeFromSuperview];//please help me to make clear background of uisearchbar
        break; 
    }
}
[self.searchBarHome setBackgroundColor:[UIColor clearColor]];
ios objective-c uisearchbar
13个回答
33
投票
对于iOS7 +,您需要做的是:

[self.searchBarHome setBackgroundColor:[UIColor clearColor]]; [self.searchBarHome setBarTintColor:[UIColor clearColor]]; //this is what you want

NOTE:这不适用于iOS6


对于iOS6 +,即使在iOS7中也将由以下人员处理:

[self.searchBarHome setBackgroundColor:[UIColor clearColor]]; [self.searchBarHome setBackgroundImage:[UIImage new]]; [self.searchBarHome setTranslucent:YES];


1
投票
extension UISearchBar { func changeSearchBarColor(fieldColor: UIColor, backColor: UIColor, borderColor: UIColor?) { UIGraphicsBeginImageContext(bounds.size) backColor.setFill() UIBezierPath(rect: bounds).fill() setBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIBarPosition.any, barMetrics: .default) let newBounds = bounds.insetBy(dx: 0, dy: 8) fieldColor.setFill() let path = UIBezierPath(roundedRect: newBounds, cornerRadius: newBounds.height / 2) if let borderColor = borderColor { borderColor.setStroke() path.lineWidth = 1 / UIScreen.main.scale path.stroke() } path.fill() setSearchFieldBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIControlState.normal) UIGraphicsEndImageContext() } }

0
投票
对于iOS 12,swift 4.x有点古怪,但是Debug视图层次结构显示_UISearchBarSearchFieldBackgroundView对背景颜色负责。要实现它,...

0
投票
仅使用searchBar.searchBarStyle = .minimal对我有用。

0
投票
如果您想更改此...

20
投票
这是我在Swift 3(Scarafone和Andrew2M的答案的组合)中的解决方案:

for view in searchBar.subviews.last!.subviews { if type(of: view) == NSClassFromString("UISearchBarBackground"){ view.alpha = 0.0 } }

或替代:

searchBar.barTintColor = UIColor.clear searchBar.backgroundColor = UIColor.clear searchBar.isTranslucent = true searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)


15
投票
UISearchBar包含两个子视图,它们是'UISearchBarBackground'和'UITextField'。在IOS8中,您需要删除“ UISearchBarBackground”以将其清除。

for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } }


11
投票
删除UISearchBarBackground可能会导致未知行为,尤其是当您开始以更高级的方式使用该元素时。

Ruozi的答案是获取UISearchBarBackground元素的首选方法,但是我建议将alpha设置为0,而不是使用以下方法删除视图:

for (UIView *subview in [[self.searchBar.subviews lastObject] subviews]) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview setAlpha:0.0]; break; } }

将以上代码添加到包含IBOutlet * searchBar的UIViewController子类中。此代码段将为iOS8和iOS9提供透明的背景。

此外,为了避免使viewController混乱,在UISearchBar子类中包含此代码可能是更好的设计决策。


9
投票
对于任何试图找到一种非骇客解决方案的人,只需在您的Storyboard / Xib文件上将背景图像设置为nil。从字面上看,只需在UISearchBar的背景图像字段中输入nil

4
投票
设置backgroundColor和barTintColor在iOS 9+上不起作用,我的背景为黑色。但是,若子解决方案将起作用。这是相同代码的快速版本。

4
投票
我的解决方案在

Swift 4.1 | iOS 11.4


3
投票
对于任何来这里但无法为您提供删除或alpha解决方案的人,请确保您在MINIMALIST上具有searchBar SearchStyle

NOT


1
投票
子类化uisearchbar,并覆盖initWithFrame和initWithCoder方法,并将背景色设置为
© www.soinside.com 2019 - 2024. All rights reserved.