UISearchBar对于新手来说 如果需要改变里面得内容图片需要查找大量的资料,现在整理出来,以供参考
1.创建
_mySearchBar = [[UISearchBar alloc] init];
//_mySearchBar.frame=CGRectMake(0, 0, SCREEN_WIDTH, 40);
_mySearchBar.delegate = self;
// //设置选项
//[_mySearchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"First",@"Last",nil]];
[_mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[_mySearchBar sizeToFit];
_mySearchBar.backgroundColor = ZCColor(23, 25, 28);
_mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:_mySearchBar.bounds.size];
//可以改变取消按钮的颜色
_mySearchBar.tintColor=ZCColor(240, 208, 122);
_mySearchBar.placeholder=@"请输入球场名称";
//改变输入字体的颜色
UITextField *searchField=[_mySearchBar valueForKey:@"_searchField"];
searchField.textColor=ZCColor(240, 208, 122);
//提示语的字体颜色
[searchField setValue:ZCColor(240, 208, 122) forKeyPath:@"_placeholderLabel.textColor"];
[_mySearchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"sousuokuang"] forState:UIControlStateNormal];
//修改提示语左边的图片
[_mySearchBar setImage:[UIImage imageNamed:@"sousuo_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
这里有个灰色的蒙版必须移除不然改变不了背景色
//移除灰色模板背景
for (UIView *subview in [[_mySearchBar.subviews firstObject] subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
//加入列表的header里面
self.tableView.tableHeaderView = _mySearchBar;
下面是创建搜索框自带的点击搜索时出现得tableview,搜索结果会出现在这个控制器上,具体代码方法和用法 下篇介绍
_mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_mySearchBar contentsController:self];
//_mySearchDisplayController = [[UISearchDisplayController alloc] init];
_mySearchDisplayController.delegate = self;
// searchResultsDataSource 就是 UITableViewDataSource
_mySearchDisplayController.searchResultsDataSource = self;
// searchResultsDelegate 就是 UITableViewDelegate
_mySearchDisplayController.searchResultsDelegate = self;
// [self.searchDisplayController setActive:NO animated:NO];
_mySearchDisplayController.searchResultsTableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"suoyou_bj_02"]];