修改UISearchBar的背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去
seachBar=[[UISearchBar alloc] init];
seachBar.backgroundColor=[UIColor clearColor];
for (UIView *subview in seachBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
添加searchBar的背景图片
searchBar.delegate = self;
searchBar.barStyle = UIBarStyleBlackTranslucent;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchBar.placeholder = _(@"Search");
searchBar.keyboardType = UIKeyboardTypeDefault;
//为UISearchBar添加背景图片
UIView *segment = [searchBar.subviews objectAtIndex:0];
UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_bar_bg.png"]];
[seachBar addSubview: bgImage];
[self.view addSubview:searchBar];
关闭键盘
[searchBar resignFirstResponder];