UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField.
方法一:是直接将 UISearchBarBackGround移去。
方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色
核心代码如下:
-(void)removeBgGroundView
{
//1.
// [[self.subviews objectAtIndex:0]removeFromSuperview];
//2.
for (UIView *subview in self.subviews)
{
NSLog(@"%@,",self.subviews);
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
//3自定义背景
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
imageView.backgroundColor = [UIColor whiteColor];
[self insertSubview:imageView atIndex:1];
}