iOS App开发中UISearchBar搜索栏组件的基本用法整理
作者:iOS UI
基本属性
@UISearchBar search = [[UISearchBar alloc]initWithFrame:CGRectMake(0,44,320,120)];
pragma mark -基本设置
//控件的样式 默认--0白色,1是黑色风格
/*
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
search.barStyle =UIBarStyleDefault;
/*
UISearchBarStyleDefault,
// currently UISearchBarStyleProminent
UISearchBarStyleProminent, // used my Mail, Messages and Contacts(provides no default background color or image but will display one if customized as such系统提供的颜色和图片无效,自定制有效)
UISearchBarStyleMinimal // used by Calendar, Notes and Music
*/
search.searchBarStyle =UISearchBarStyleDefault;
// 控件上面的显示的文字
search.text =@"HMT";
// 显示在顶部的单行文字,通常作为一个提示行
search.prompt =@"DOTA";
// 半透明的提示文字,输入搜索内容消失
search.placeholder =@"请输入要搜索的词语";
// bar的颜色(具有渐变效果)搜索栏闪动条和选择栏边框,取消按钮和选择栏被选中时候都会变成设置的颜色
search.tintColor = [UIColor redColor];
// 除搜索栏框框,就像贴了一张镂空了搜索栏的颜色贴图,不影响其他任何设置的颜色
search.barTintColor = [UIColor whiteColor];
// 指定控件是否会有透视效果
search.translucent =YES;
// 设置在什么的情况下自动大写
/*
UITextAutocapitalizationTypeNone, //除非自己点击大写,否则永不大写
UITextAutocapitalizationTypeWords, //以单词来区分,每个单词首字母大写
UITextAutocapitalizationTypeSentences, //以句子来区分
UITextAutocapitalizationTypeAllCharacters, //所有字母全部大写
*/
search.autocapitalizationType =UITextAutocapitalizationTypeNone;
// 对于文本对象自动校正风格(额,我也不知道有什么用)
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
search.autocorrectionType =UITextAutocorrectionTypeNo;
// 键盘的样式(具体可参考文章UITableView详解(一))
search.keyboardType =UIKeyboardTypeNumberPad;
pragma mark - 设置搜索栏右边按钮图标(UISearchBarIcon)
// 是否在控件的右端显示一个书的按钮
search.showsBookmarkButton =YES;
// 是否显示cancel按钮(静态)
//search.showsCancelButton = YES;
// 是否显示cancel按钮(带有动画效果)
[search setShowsCancelButton:YES animated:YES];
// 是否在控件的右端显示搜索结果按钮(图形是一个圆里面放着一个向下的箭头)
search.showsSearchResultsButton =YES;
// 搜索结果按钮是否被选中
search.showsSearchResultsButton =YES;
// 设置控件的右端显示搜索结果按钮处 --- 可用图片替换掉
[search setImage:[UIImage imageNamed:@"qiyi.png"]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
pragma mark - 搜索栏下部选择栏
// 搜索栏下部的选择栏,数组里面的内容是按钮的标题
search.scopeButtonTitles = [NSArray arrayWithObjects:@"iOS",@"Android",@"iPhone",nil];
// 进入界面,搜索栏下部的默认选择栏按钮的索引(也就是第一出现在哪个选择栏)
search.selectedScopeButtonIndex =2;
// 控制搜索栏下部的选择栏是否显示出来(显示的话,就要修改search的frame,不显示的话80就够了)
search.showsScopeBar =YES;
pragma mark - 设置控件图片
// 设置控件背景图片
search.backgroundImage = [UIImage imageNamed:@"qiyi.png"];
// 设置搜索栏下部背景图片
search.scopeBarBackgroundImage = [UIImage imageNamed:@"qiyi.png"];
pragma mark - 协议UISearchBarDelegate
(不解释了,看名字,已经很明显了)
@编辑文本
// UISearchBar得到焦点并开始编辑时,执行该方法
(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ // called when text starts editing
[searchBar setShowsCancelButton:YES animated:YES]; // 动画显示取消按钮
}
(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ // called when text changes (including clear)
@ 当搜索内容变化时,执行该方法。很有用,可以实现时实搜索
}
(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0); // called before text changes
@按钮点击
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // called when bookmark button pressed
(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{ // called when cancel button pressed
[searchBar setShowsCancelButton:NO animated:NO]; // 取消按钮回收
[searchBar resignFirstResponder]; // 取消第一响应值,键盘回收,搜索结束
}
(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2);// called when search results button pressed
(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0);
数据刷选类:NSPredicate
@假设: NSArray array = [[NSArray alloc]initWithObjects:@"luna",@"moon",@"",@"lion",@"coco", nil];
// 数据的处理主要发生在这个方法中
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
// 方法一:([c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。)
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchText];
// 数组提供的快速遍历,返回的类型是NSArray
NSLog(@"%@",[ _array filteredArrayUsingPredicate:predicate]);
// 方法二:
for (int i = 0; i count]; i++) {
if ([predicate evaluateWithObject:[ _array objectAtIndex:i]]) {
NSLog(@"%@",[arrayobjectAtIndex:i]);
}
}
}