关于UISearchBar在tableHeaderView的情况
self.tableView.tableHeaderView = self.searchBar;
- (UISearchBar *)searchBar {
if (_searchBar == nil)
{
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-80, 40)];
_searchBar.placeholder = @"搜索";
_searchBar.searchBarStyle = 0;
_searchBar.delegate = self;
// 边框颜色
_searchBar.backgroundImage = [self createImageWithColor:kRGBA(242, 242, 242, 1)];
UITextField *searchField = [_searchBar valueForKey:@"searchField"];
if (searchField) {
[searchField setBackgroundColor:[UIColor whiteColor]];
[searchField setValue:kGrayColor forKeyPath:@"_placeholderLabel.textColor"];
// 修正光标颜色
[searchField setTintColor:kGrayColor];
}
}
return _searchBar;
}
#pragma mark - 根据颜色获取图片
- (UIImage *)createImageWithColor:(UIColor *)color
{
//图片尺寸
CGRect rect = CGRectMake(0, 0, 10, 10);
//填充画笔
UIGraphicsBeginImageContext(rect.size);
//根据所传颜色绘制
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
//显示区域
CGContextFillRect(context, rect);
// 得到图片信息
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//消除画笔
UIGraphicsEndImageContext();
return image;
}
代理方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
//隐藏取消按钮
searchBar.showsCancelButton = NO;
return YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// 当text改变 就会调用
}