輸入相關(guān)關(guān)鍵字在百度中能搜索出來(lái)很多,但是都不靠譜费就,于是Google了一下润文,在StackOverFlow中找到一個(gè)蠻靠譜的方法,試了可行豁跑。
1.for(UIView *subviewinsearchBar.subviews) {for(UIView* grandSonViewinsubview.subviews){if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
grandSonView.alpha=0.0f;
}elseif([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
NSLog(@"Keep textfiedld bkg color");
}else{
grandSonView.alpha=0.0f;
}
}//for cacheViews}//subviews
接下來(lái)廉涕,我又發(fā)現(xiàn)一位博主分享的文章,寫(xiě)的很詳細(xì),而且解決方案看起來(lái)會(huì)更加優(yōu)雅狐蜕,不像上面給的那樣暴力粗魯宠纯。^_^
文章在這里。
這里貼出代碼:
- (void)viewDidLoad
{
[super viewDidLoad];//1UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:32.0f];//2[_searchBar setBackgroundImage:clearImg];//3[_searchBar setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];//4[_searchBar setBackgroundColor:[UIColor clearColor]];
}+ (UIImage*) imageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
CGRect rect= CGRectMake(0.0f,0.0f,1.0f, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();returnimage;
}
修改APP內(nèi)全局UISearchBar樣式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIColor* myColor = [UIColor colorWithRed:0green:0.48blue:1alpha:1];//set your desired background color hereUIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];
UIImage* coloredImg = [CDViewController imageWithColor:myColor andHeight:32.0f];
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UISearchBar appearance] setBackgroundImage:clearImg];
[[UISearchBar appearance] setSearchFieldBackgroundImage:coloredImg forState:UIControlStateNormal];returnYES;
}
給ScopeBar設(shè)置透明背景:
UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];
[_searchBar setScopeButtonTitles:nil];
[_searchBar setScopeBarBackgroundImage:clearImg];
[_searchBar setScopeBarButtonBackgroundImage:clearImg forState:UIControlStateNormal];
[_searchBar setScopeBarButtonDividerImage:clearImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];