iOS7,UISearchBar 的背景图片

标签 ios ios7 uikit transition searchbar

我正在 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 又回来了。

How it looks

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

编辑问题

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

实际上,我确实在这里和那里使用了 barTintColor 和其他选项,但它不起作用。这是将 barTintColor 设置为相同颜色的结果。但是上面有这个白色层 enter image description here

最佳答案

在 iOS 7 中,属性 backgroundImagescopeBarBackgroundImage 不再按预期工作并变为半透明。

在 iOS 7 中引入了以下方法来解决这个问题。 (文档 here)

setBackgroundImage:forBarPosition:barMetrics:

这是你应该做的:

 [self.searchDisplayController.searchBar setBackgroundImage:[self imageWithColor:[UIColor yourColor]] 
                                             forBarPosition:0 
                                                 barMetrics:UIBarMetricsDefault];

在这里,barPosition : 0UIBarPositionAny

编辑:

快速代码:

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

关于iOS7,UISearchBar 的背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927542/

相关文章:

swift - 添加阴影以仅用两个带半径的角进行 View

iphone - 释放 UIViewController

ios - 删除 Storyboard中添加的对象

iOS 圆角个人资料图像的干净角落

ios - 如何在Mac上的iPhone Simulator上运行ElectronJS应用程序?

ios - iOS 中是否有最大图像宽度(我的图像是 25020 x 238)?调整大小时图像有效

iphone - iOS 7多空格问题

ios - HTML5 离线应用总是在 iPod Touch iOS 4.2.1 上发送 "error"事件

javascript - 禁用 IOS Safari 弹性滚动

swift - 有没有办法在 Swift 包中包含 header