(我知道这可能看起来是重复的,但我告诉你我在这里发现的所有内容都没有)
我有一个带有搜索显示 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/