iOS7,UISearchBar的backgroundImage

Ger*_*.ch 25 transition uikit ios searchbar ios7

我正在iOS 6和iOS 7之间进行UI转换.

我们有一个与UISearchDisplayController相关的UISearchBar,我已经将navigationBar的backgroundImage和searchBar设置为用颜色动态创建的1x1图像.

self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchDisplayController.searchBar.tintColor = [UIColor myTintColor];
self.searchDisplayController.searchBar.backgroundImage = [self imageWithColor:[UIColor myBGColor]];
self.searchDisplayController.searchBar.scopeBarBackgroundImage = [self imageWithColor:[UIColor myBGColor]];
Run Code Online (Sandbox Code Playgroud)

在iOS6上,一切都按预期工作.在iOS7上,当选择了searchBar时,scopeBar会出现好的backgroundImage(使用searchBar.scopeBarBackgroundImage设置),但searchBar是一种半透明的灰色.当我按下取消时,searchBar backgroundImage又回来了.

看起来如何

////////////////////////////////////////////////// ////////////////////////////////////////////////// /

编辑问题

////////////////////////////////////////////////// ////////////////////////////////////////////////// /

实际上,我确实在这里和那里使用了barTintColor和其他选项,但它不起作用.这是barTintColor设置为相同颜色的结果.但是顶部有这个白色层 在此输入图像描述

aks*_*h1t 41

在iOS系统7,属性backgroundImagescopeBarBackgroundImage不再按预期工作,并成为半透明.

iOS 7中引入了以下方法来解决此问题.(文件在这里)

setBackgroundImage:forBarPosition:barMetrics:
Run Code Online (Sandbox Code Playgroud)

这是你应该做的:

 [self.searchDisplayController.searchBar setBackgroundImage:[self imageWithColor:[UIColor yourColor]] 
                                             forBarPosition:0 
                                                 barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

在这里,barPosition : 0UIBarPositionAny.

编辑:

SWIFT代码:

self.searchDisplayController.searchBar.setBackgroundImage(self.image(color: UIColor.yourColor), for: UIBarPosition(rawValue: 0)!, barMetrics:.default)
Run Code Online (Sandbox Code Playgroud)