//创建一个searchBar对象
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70, 300, 30)];
searchBar.backgroundColor = [UIColor redColor];
[self.view addSubview:searchBar];
//设置搜索框的背景颜色
searchBar.barStyle = UIBarStyleBlack;
//设置目前的或要搜索的内容
searchBar.text = @"search";
//设置提示信息
searchBar.prompt = @"提示信息";
//设置占位符
searchBar.placeholder = @"请输入要搜索的信息";
//设置显示书签按钮(当没有输入内容时显示)
searchBar.showsBookmarkButton = YES;
//设置显示取消按钮
searchBar.showsCancelButton = YES;
// 控制搜索栏下部的选择栏是否显示出来
searchBar.showsScopeBar = YES;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"BOY",@"GIRL",@"ALL",nil];
//当给搜索框输入信息时,给键盘添加一个自定义视图
UIView *view = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
view.backgroundColor = [UIColor redColor];
searchBar.inputAccessoryView = view;
//设置显示查询结果按钮
searchBar.showsSearchResultsButton = YES;
//设置搜索栏中的字体颜色
searchBar.tintColor = [UIColor redColor];
//设置搜索框的背景颜色
searchBar.barTintColor = [UIColor cyanColor];
//设置搜索框的样式
searchBar.searchBarStyle = UISearchBarStyleMinimal;
//设置搜索框的键盘样式
searchBar.keyboardType = UIKeyboardTypeNumberPad;
//设置代理
searchBar.delegate = self;
}
//实现代理方法
//点击了取消按钮
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"点击了取消按钮");
}
//点击了结果按钮
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"点击了结果按钮");
}
//开始编辑文件
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"开始编辑");
}
//结束编辑文件
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@"结束编辑");
}
//内容改变
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"查询内容改变");
}
//选中某个scope按钮执行该方法
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
NSLog(@"%@",[searchBar.scopeButtonTitles firstObject]);
}
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70, 300, 30)];
searchBar.backgroundColor = [UIColor redColor];
[self.view addSubview:searchBar];
//设置搜索框的背景颜色
searchBar.barStyle = UIBarStyleBlack;
//设置目前的或要搜索的内容
searchBar.text = @"search";
//设置提示信息
searchBar.prompt = @"提示信息";
//设置占位符
searchBar.placeholder = @"请输入要搜索的信息";
//设置显示书签按钮(当没有输入内容时显示)
searchBar.showsBookmarkButton = YES;
//设置显示取消按钮
searchBar.showsCancelButton = YES;
// 控制搜索栏下部的选择栏是否显示出来
searchBar.showsScopeBar = YES;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"BOY",@"GIRL",@"ALL",nil];
//当给搜索框输入信息时,给键盘添加一个自定义视图
UIView *view = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
view.backgroundColor = [UIColor redColor];
searchBar.inputAccessoryView = view;
//设置显示查询结果按钮
searchBar.showsSearchResultsButton = YES;
//设置搜索栏中的字体颜色
searchBar.tintColor = [UIColor redColor];
//设置搜索框的背景颜色
searchBar.barTintColor = [UIColor cyanColor];
//设置搜索框的样式
searchBar.searchBarStyle = UISearchBarStyleMinimal;
//设置搜索框的键盘样式
searchBar.keyboardType = UIKeyboardTypeNumberPad;
//设置代理
searchBar.delegate = self;
}
//实现代理方法
//点击了取消按钮
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"点击了取消按钮");
}
//点击了结果按钮
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"点击了结果按钮");
}
//开始编辑文件
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"开始编辑");
}
//结束编辑文件
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@"结束编辑");
}
//内容改变
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"查询内容改变");
}
//选中某个scope按钮执行该方法
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
NSLog(@"%@",[searchBar.scopeButtonTitles firstObject]);
}