一、UISearchBar 的界面
1、属性searchBarStyle对应的界面
2、设置UISearchBar的背景色
代码:
searchBar.backgroundColor = [UIColor blackColor];
效果:
如果想设置默认情况下的背景色,则可以通过设置背景图片来实现
先添加方法:
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
再添加代码:
searchBar.backgroundImage = [self imageWithColor:[UIColor blackColor] size:searchBar.bounds.size];
2、设置UISearchBar的文字的颜色
方法一:
for (UIView *subView in self.searchBar.subviews)
{
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UITextField class]])
{
UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
//set font color here
searchBarTextField.textColor = [UIColor colorWithWhite:0.799 alpha:1.000];
break;
}
}
}
方法二:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
一、UISearchDisplayController用法
mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
mySearchDisplayController.delegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.searchResultsDelegate = self;
注:UISearchDisplayController 的结果显示依赖于他的代理的tableView样式,在searchBar的代理方法中直接对数据源的数据进行改变。最后重新加载数据
[mySearchDisplayController.searchResultsTableView reloadData];
[_tableView reloadData];
即可还原搜索前的数据。
另外,为了保证健壮性,最好是在tableview的cellForRow方法中加个判断
if (_dataArr.count > indexPath.row){
******
}