@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);
- 前言
- UITableView详解(UITableViewCell(一)重中之重)
- UITableView详解(UITableViewCell(二) 自定义cell)
- UITableView详解(UITableViewCell(三) cell根据文本长度来自动调整cell高度)
- UITableView详解(UITableViewCell(四) 增加 删除 移动)
- UITabBarController详解(一)UITabBarController的介绍和设置(偷了点懒,直接用了ARC)
- UITabBarController详解(二)UITabBarController的代理方法以及模态显示
- UISearchBar详解(一)基本属性
- UISearchBar详解(二)数据刷选类:NSPredicate
- UISearchDisplayController 的使用
- UINavigationController详解(一)
- UINavigationController详解(二)UINavigationBar(UIBarButtonItem)
- UINavigationController详解(三)UIToolBar
- UINavigationController详解(四)iOS7新特性
- UIScrollView控件详解
- UISwitch用法-以及-自定义UISwitch控件
- UIAlertView用法
- UILabel 的常见属性和方法:
- UIPickerView(滚动选择控制器)
- UIActivityIndicatorView(活动指示器 ---------> 网络卡后加载,画面,图像加载闪烁的圆圈)
- UIStepper
- UIImagePickerController--------图片选取器
- UITextView
- UITabBarController详解(三)自定义UITabBarController
- UIWebView基本介绍