1、去掉 UISearchBar 的背景
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
或者
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
2、修改 UISearchBar 的背景
UIView *segment = [searchBar.subviews objectAtIndex:0];
UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.jpg"]];
[segment addSubview: bgImage];
3、在 tableview 上添加 UISearchBar
//動態添加 TableView
UITableView *tableView =
[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor whiteColor];
tableView.delegate=self;
tableView.dataSource=self;
[tableView setRowHeight:40];
//動態添加 SearchBar
UISearchBar searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];
searchBar.placeholder=@"Enter Name";
searchBar.delegate = self;
tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
[searchBar release];
[self.view addSubview:tableView];
[tableView release];
4、實現 UISearchBarDelegate 來完成 UISearchBar 的功能性部分
可參考:http://blog.csdn.net/iukey/article/details/7318682
UISearchBar 更多的功能定製查看協議 UISearchBarDelegate 的定義。