iOS App开发中UISearchBar搜索栏组件的基本用法整理
基本属性
@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]);
}
}
}