修改系统搜索框
//searchbar
//设置背景图是为了去掉上下黑线
self.searchBar.backgroundImage = [[UIImage alloc] init];
// 设置SearchBar的颜色主题为白色
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.backgroundColor = [UIColor whiteColor];
self.searchBar.frame = CGRectMake( 0, self.bgImageView.bottom, __kScreenWidth, 40);
self.searchBar.placeholder = @"输入名称搜索,如:京东";
[self.searchBar setImage:[UIImage imageNamed:@"salecard_search"]forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[self.view addSubview:self.searchBar];
另设置搜索框取消按钮代理方法
#pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦)
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
// 修改UISearchBar右侧的取消按钮文字颜色及背景图片
for (UIView *searchbuttons in [searchBar subviews]){
if ([searchbuttons isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)searchbuttons;
// 修改文字颜色
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
// 修改按钮背景
[cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted];
}
}
}