我在SO上看到此帖子:UISearchBar solid color如何在UISearchBar上應用純色
但是沒有提供答案。
我想知道如何將純色應用於UISearchBar。我應用了一個tintColor,但它會自動創建一個漸變。
我在SO上看到此帖子:UISearchBar solid color如何在UISearchBar上應用純色
但是沒有提供答案。
我想知道如何將純色應用於UISearchBar。我應用了一個tintColor,但它會自動創建一個漸變。
您可以覆蓋搜索欄的drawRect。通過將「UISearchBarBackground」視圖設置爲0的alpha值,它將爲您設置的平坦色調顏色繪製它。例如,在應用程序委託補充一點:
@implementation UISearchBar(CustomBackground)
- (void)drawRect:(CGRect)rect
{
// Find the UISearchBarBackground view and set it's alpha to 0 so we get a "flat"
// search bar.
for(UIView *subview in self.subviews)
{
if([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview setAlpha:0.0F];
break;
}
}
}
@end
我這樣做:
[toolbar setTintColor:[UIColor greenColor]];
,並得到這樣的:
這就是你想要的?
差不多,但它並不適用於一個UISearchBar – 2010-08-17 03:38:13
我還沒有嘗試過的代碼的工作,但在IB設置着色顏色正常工作(但不不解決純色問題,仍然有一個漸變圖層) – 2013-04-01 07:21:51
_tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
_tempSearchBar.barStyle=UIBarStyleDefault;
[email protected]"搜索";
for(UIView *subview in _tempSearchBar.subviews)
{
if([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
UIView *aView = [[UIView alloc] initWithFrame:subview.bounds];
aView.backgroundColor = [UIColor greenColor];
// [subview setAlpha:0.0f];
[subview addSubview:aView];
break;
}
}
添加這會導致我的搜索欄背景變黑,而不是我嘗試應用的顏色。任何想法爲什麼? – jmosesman 2012-07-16 18:33:12
我還需要更改搜索欄的背景顏色,在UISearchBar上給出所需的純色,謝謝。 – 2013-04-01 09:18:47