iOS7,UISearchBar 的 backgroundImage

iOS7, backgroundImage for UISearchBar(iOS7,UISearchBar 的 backgroundImage)

本文介绍了iOS7,UISearchBar 的 backgroundImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making the transition of the UI between iOS 6 and iOS 7.

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

We have a UISearchBar related to a UISearchDisplayController, I have set the backgroundImage of the navigationBar and the searchBar to a 1x1 image dynamically created with a color.

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 上,一切正常.在 iOS7 上,当 searchBar 被选中时,scopeBar 出现了很好的 backgroundImage(用 searchBar.scopeBarBackgroundImage 设置)但是 searchBar 是一种半透明的灰色.当我按下 Cancel 时,searchBar backgroundImage 又回来了.

On iOS6, everything works as expected. 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. When I press on Cancel, the searchBar backgroundImage is back.

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

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

编辑问题

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

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

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

Actually, I did use the barTintColor and other options here and there but it doesn't work. That is the result with the barTintColor set with the same color. But there is this white layer on top

推荐答案

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

In iOS 7, the properties backgroundImage and scopeBarBackgroundImage no longer work as expected and become translucent.

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

The following method has been introduced in iOS 7 which addresses this problem. (Docs here)

setBackgroundImage:forBarPosition:barMetrics:

这是你应该做的:

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

这里,barPosition : 0UIBarPositionAny.

Swift 代码:

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

这篇关于iOS7,UISearchBar 的 backgroundImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS7,UISearchBar 的 backgroundImage

基础教程推荐