UISearchBar介绍

属性 作用
UIBarStyle barStyle 控件的样式
id delegate 设置控件的委托
NSString *text 控件上面的显示的文字
NSString *prompt 显示在顶部的单行文字,通常作为一个提示行
NSString *placeholder 半透明的提示文字,输入搜索内容消失
BOOL showsBookmarkButton 是否在控件的右端显示一个书的按钮(没有文字的时候)
BOOL showsCancelButton 是否显示cancel按钮
BOOL showsSearchResultsButton 是否在控件的右端显示搜索结果按钮(没有文字的时候)
BOOL searchResultsButtonSelected 搜索结果按钮是否被选中
UIColor *tintColor bar的颜色(具有渐变效果)
BOOL translucent 指定控件是否会有透视效果
UITextAutocapitalizationType
autocapitalizationType
设置在什么的情况下自动大写
UITextAutocorrectionType
autocorrectionType
对于文本对象自动校正风格
UIKeyboardType
keyboardType
键盘的样式
NSArray *scopeButtonTitles 搜索栏下部的选择栏,数组里面的内容是按钮的标题
NSInteger selectedScopeButtonIndex 搜索栏下部的选择栏按钮的个数
BOOL showsScopeBar 控制搜索栏下部的选择栏是否显示出来

代理列表:

编辑代理

点击按钮

范围代理

searchBar使用小技巧

searchBar的范围控件showsScopeBar,官方学名叫Scope Buttons。
首先就要设置这个属性:
self.searchBar.showsScopeBar = YES;
然后要给他添加按钮。比如说,这样:self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"BOY",@"GIRL",@"ALL",nil];
还有一个很重要的事情就是我们要实现这个代理UISearchBarDelegate里的这个方法searchBar:selectedScopeButtonIndexDidChange:。告诉表格,你选择的范围是啥。
还有要是设置默认选择哪个按钮的话,要设置这个属性,像这样就是默认选中第1个啦。
self.searchBar.selectedScopeButtonIndex = 0;

在实现搜索功能时,界面使用UISearchBar比较好,它实现了很多搜索时使用到的东西,但是默认的风格可能和现有的风格不一致,所以需要我们想办法去修改一下默认的外观。

1、修改UISearchBar的背景颜色

UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去


  1. seachBar=[[UISearchBar alloc] init];
  2. seachBar.backgroundColor=[UIColor clearColor];
  3. for (UIView *subview in seachBar.subviews)
  4. {
  5. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
  6. {
  7. [subview removeFromSuperview];
  8. break;
  9. }
  10. }

第二种解决的方法:


  1. [[searchbar.subviews objectAtIndex:0]removeFromSuperview];

2、


  1. UISearchBar* m_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 44, 320, 41)];
  2. m_searchBar.delegate = self;
  3. m_searchBar.barStyle = UIBarStyleBlackTranslucent;
  4. m_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
  5. m_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
  6. m_searchBar.placeholder = _(@"Search");
  7. m_searchBar.keyboardType =  UIKeyboardTypeDefault;
  8. //为UISearchBar添加背景图片
  9. UIView *segment = [m_searchBar.subviews objectAtIndex:0];
  10. UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Images/search_bar_bg.png"]];
  11. [segment addSubview: bgImage];
  12. //<</span>---背景图片
  13. [self.view addSubview:m_searchBar];
  14. [m_searchBar release];

3:取消UISearchBar调用的键盘


  1. [searchBar resignFirstResponder];

添加UISearchBar的两种方法:

代码


  1. UISearchBar *mySearchBar = [[UISearchBar alloc]
  2. initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
  3. mySearchBar.delegate = self;
  4. mySearchBar.showsCancelButton = NO;
  5. mySearchBar.barStyle=UIBarStyleDefault;
  6. mySearchBar.placeholder=@"Enter Name or Categary";
  7. mySearchBar.keyboardType=UIKeyboardTypeNamePhonePad;
  8. [self.view addSubview:mySearchBar];
  9. [mySearchBar release];

在 tableview上添加:

代码


  1. //add Table
  2. UITableView *myBeaconsTableView = [[UITableView alloc]
  3. initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40)
  4. style:UITableViewStylePlain];
  5. myBeaconsTableView.backgroundColor = [UIColor whiteColor];
  6. myBeaconsTableView.delegate=self;
  7. myBeaconsTableView.dataSource=self;
  8. [myBeaconsTableView setRowHeight:40];
  9. // Add searchbar
  10. searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];
  11. searchBar.placeholder=@"Enter Name";
  12. searchBar.delegate = self;
  13. myBeaconsTableView.tableHeaderView = searchBar;
  14. searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
  15. searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
  16. [searchBar release];
  17. [self.view addSubview:myBeaconsTableView];
  18. [myBeaconsTableView release];
 
  1. UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去 

     

     

UISearchBar总结的更多相关文章

  1. iOS 如何自定义UISearchBar 中textField的高度

    iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...

  2. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  3. 更改UIsearchbar 的背景和cancel按钮(转)

    修改背景 searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(,KTopBarHeight, , KTopBarHeight)]; sear ...

  4. UISearchBar控件-让我们来搞定!(转)

    转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html     最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好 ...

  5. iOS开发——UI进阶篇(十九)UISearchBar控件简介

    最近用到搜索功能.总结一下 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你 ...

  6. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:  这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让 ...

  7. 修改UISearchBar的背景颜色

    当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ...

  8. iOS UISearchBar 设置取消按钮,回收键盘,并修改cancel为“取消”

    继承协议: UISearchBarDelegate 在代理方法中设置: #pragma mark --- 搜索框开始编辑 --- - (void)searchBarTextDidBeginEditin ...

  9. iOS开发之直接使用UISearchBar

    iOS开发中经常需要使用SearchBar,我们可以选择使用UISearchBar+UISearchController或者UISearchBar+UISearchDisplayController( ...

  10. 自定义UISearchBar

    先上系统默认的UISearchBar,然后用KVO修改 UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:_topView.boun ...

随机推荐

  1. 如何删除Oracle数据库

    1>点击开始找Oracle的目录,-->点击[Universal Installer],打开点击[卸载产品] 2>除了oracle_home1 不点外,其他的都勾选. 3>再点 ...

  2. oracle update语句的几点写法

    update两表关联的写法包括字查询 1.update t2 set parentid=(select ownerid from t1 where t1.id=t2.id); 2. update tb ...

  3. MS SQLSERVER通用存储过程分页

    最近在面试的时候,遇到个奇葩的秃顶老头面试官. 问:写过存储过程分页吗? 答:没写过,但是我知道分页存储的原理,我自己也写过,只是在工作中没写过. 问:那你这么多年工作中就没写过吗? 答:的确没写过, ...

  4. 64位ubuntu编译32位程序

      最近在64位ubuntu上开发,需要编译32位程序,需要安装这两个包,然后在编译器参数加上-m32.不放心的话可以用ldd或file查看一下是否生成了对应位数的程序. $ apt-get inst ...

  5. 汇编语言学习系列 for循环实现

    假如汇编语言要实现如下C语言的功能,编译环境Ubuntu14.04(32位). #include<stdio.h> int fact_for(int n) { int i; ; ; i & ...

  6. mac os vim 乱码

    yum -y groupinstall chinese-support vim /etc/sysconfig/i18n LANG="zh_CN.UTF-8" LANGUAGE=&q ...

  7. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...

  8. cookie记录用户的浏览商品的路径

    在电子商务的网站中,经常要记录用户的浏览路径,以判断用户到底对哪些商品感兴趣,或者哪些商品之间存在关联. 下面将使用cookie记录用户的浏览过的历史页面.该网站将每个页面的标题保存在该页面的$TIT ...

  9. eclipse 快捷键汇总

    1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索对话框:Ctrl+H快速O ...

  10. Entity Framework 数据部分更新之Attach &&Detach

    我们经常会遇到这样的问题:Update一个entity的部分数据时,通常需要new一个新的对象,然后事这新的对象Attach到Context中,代码如下所示: /// <summary> ...