ios - UISearchBar 将半透明设置为 NO 不起作用

标签 ios uitableview uisearchbar

(我知道这可能看起来是重复的,但我告诉你我在这里发现的所有内容都没有)

我有一个带有搜索显示 Controller 和一个搜索栏的 UITableView,我正在尝试根据公司颜色设置 tintColor。但无论我尝试什么,它都是半透明的。

这是示例代码:

[self.searchDisplayController.searchBar setTranslucent:NO];
[self.searchDisplayController.searchBar setBarTintColor:[[ConfToolbox sharedInstance] getBarTintColor]];
[[ConfToolbox sharedInstance] getBarTintColor]返回深蓝色 UIColor .

我四处寻找答案,但没有任何效果,甚至 this accepted answer .

任何帮助都会很棒。

干杯,

阿 Jade 。

最佳答案

我用这个实现了我想要的,以防万一有人遇到同样的问题:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,10), YES, 0);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[ConfToolbox sharedInstance] barTintColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0,0,10,10));
UIImage* coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.searchDisplayController.searchBar setBackgroundImage:coloredImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

可以设置CGContextSetFillColorWithColor的第二个参数任何你想要的颜色。

来自 documentation

The default value is YES. If the search bar has a custom background image, the default is YES if any pixel of the image has an alpha value of less than 1.0, and NO otherwise.



由于图像是由 alpha 值为 1 的颜色制成的,因此半透明效果按说明关闭。

关于ios - UISearchBar 将半透明设置为 NO 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363413/

相关文章:

ios - 提示用户给出五星级评价

iphone - 如何在iPhone中实现内容搜索?

ios - 如何在iOS7中自定义UISearchBar(清除按钮)

ios - 在 "class func"方法中获取 Swift 类名

ios - 将 rootViewController 从 uiviewcontroller 更改为 uinavigationcontroller

ios - 如何将文本分配给 tableView 中的单元格?

ios - 子类 uitableview 单元格以快速更改分隔符高度

iOS UITableview 滚动中途停止

ios - iOS 7 中 UISearchBar 隐藏 View

ios - 第一个 uiwebview 显示第二个 webview 的 url