我正在 iOS 6 和 iOS 7 之间转换 UI。
我们有一个与 UISearchDisplayController 相关的 UISearchBar,我已将 navigationBar 和 searchBar 的 backgroundImage 设置为使用颜色动态创建的 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]];
在 iOS6 上,一切都按预期工作。 On iOS7, when the searchBar is selected, the scopeBar appears with the good backgroundImage (set with searchBar.scopeBarBackgroundImage) but the searchBar is a kind of translucent gray.当我按下取消时,searchBar backgroundImage 又回来了。
////////////////////////////////////////////////////////////////////////////////////////////////
编辑问题
////////////////////////////////////////////////////////////////////////////////////////////////
实际上,我确实在这里和那里使用了 barTintColor 和其他选项,但它不起作用。这是将 barTintColor 设置为相同颜色的结果。但是上面有这个白色层
最佳答案
在 iOS 7 中,属性 backgroundImage
和 scopeBarBackgroundImage
不再按预期工作并变为半透明。
在 iOS 7 中引入了以下方法来解决这个问题。 (文档 here)
setBackgroundImage:forBarPosition:barMetrics:
这是你应该做的:
[self.searchDisplayController.searchBar setBackgroundImage:[self imageWithColor:[UIColor yourColor]]
forBarPosition:0
barMetrics:UIBarMetricsDefault];
在这里,barPosition : 0
是 UIBarPositionAny
。
编辑:
快速代码:
self.searchDisplayController.searchBar.setBackgroundImage(self.image(color: UIColor.yourColor), for: UIBarPosition(rawValue: 0)!, barMetrics:.default)
关于iOS7,UISearchBar 的背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927542/