直奔主题
-(void)searchBarView
{
UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width-40, 80)];
search.delegate = self;
//占位符
search.placeholder = @"搜索";
//添加背景颜色
search.barTintColor = [UIColor clearColor];
//添加背景图片
[search setBackgroundImage:[UIImage imageNamed:@"1.jpg"]];
//光标的颜色
search.tintColor = [UIColor yellowColor];
/*
UISearchBarStyleDefault,
UISearchBarStyleProminent,
UISearchBarStyleMinimal
*/
search.searchBarStyle = UISearchBarStyleDefault;
//控件的样式 默认是0 白色 1 黑色
/*
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
*/
search.barStyle = UIBarStyleDefault;
//控件上面显示的字体;
search.text = @"sda";
//在顶端显示单行文字一般都作为一个提示
search.prompt = @"DoTo";
//控件是否有透视效果
search.translucent = YES;
// 设置在什么的情况下自动大写
/*
UITextAutocapitalizationTypeNone, //除非自己点击大写,否则永不大写
UITextAutocapitalizationTypeWords, //以单词来区分,每个单词首字母大写
UITextAutocapitalizationTypeSentences, //以句子来区分
UITextAutocapitalizationTypeAllCharacters, //所有字母全部大写
*/
search.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 对于文本对象自动校正风格(每感觉有什么用)
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
search.autocorrectionType = UITextAutocorrectionTypeNo;
//是否在控件的右端添加书的按钮
search.showsBookmarkButton = YES;
//是否显示cancel按钮
search.showsCancelButton = YES;
// 是否显示cancel按钮(带有动画效果)
[search setShowsCancelButton:YES animated:YES];
// 是否在控件的右端显示搜索结果按钮(图形是一个圆里面放着一个向下的箭头)
search.showsSearchResultsButton =YES;
// 搜索结果按钮是否被选中
search.showsSearchResultsButton =YES;
// 设置控件的右端显示搜索结果按钮处 --- 可用图片替换掉
[search setImage:[UIImage imageNamed:@"aabbcc.png"]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
#pragma mark--搜索栏下部选择栏
// 搜索栏下部的选择栏,数组里面的内容是按钮的标题
search.scopeButtonTitles = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
// 进入界面,搜索栏下部的默认选择栏按钮的索引(也就是第一出现在哪个选择栏)
search.selectedScopeButtonIndex =2;
// 控制搜索栏下部的选择栏是否显示出来(显示的话,就要修改search的frame,不显示的话80就够了)
search.showsScopeBar =YES;
// 设置搜索栏下部背景图片
search.scopeBarBackgroundImage = [UIImage imageNamed:@"aabbcc.png"];
[self.view addSubview:search];
}
#pragma mark----UISearchBarDetegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES]; // 动画显示取消按钮
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
return YES;
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
}
//当UISearchBar文本改变时候执行的方法(我一般都是用这个实时搜索 给用户的感觉也很好)
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"text ==== %@",searchText);
}
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
//按钮点击
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
}
//点击书按钮触发
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
}
//点击cancel按钮触发
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
[searchBar setShowsCancelButton:NO animated:NO]; // 取消按钮回收
[searchBar resignFirstResponder]; // 取消第一响应值,键盘回收,搜索结束
}
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
}