UISearchBar 背景色设透明

一:

- (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;

}

二:直接调,就完事了!



- (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]];

}

+(UISearchBar*)createSearchBarWithFrame:(CGRect)frame andTarget:(id) target andWithPlaceHolderTitle:(NSString *)title{

UISearchBar * searchBar = [[UISearchBar alloc]initWithFrame:frame];

searchBar.placeholder= title;

searchBar.delegate = target;

[searchBar setContentMode:UIViewContentModeBottomLeft];

searchBar.tintColor=LTColor(0, 122, 255, 1.0);

searchBar.backgroundColor = [UIColor clearColor];

for (UIView *view in searchBar.subviews) {

// for before iOS7.0

if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

[view removeFromSuperview];

break;

}

// for later iOS7.0(include)

if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {

[[view.subviews objectAtIndex:0] removeFromSuperview];

break;

}

}

return searchBar;

}

你可能感兴趣的:(UISearchBar 背景色设透明)