摘自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html

UISearchBar控件就是要为你完成搜索功能的一个专用控件。它集成了很多你意想不到的功能和特点!

首先,还是来普及一下UISearchBar控件API相关的属性和方法吧!

UISearchBar属性相关

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解释

[self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符

[self.searchBar setPrompt:@"Prompt"];// 顶部提示文本,相当于控件的Title

[self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框样式

[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色,当设置此属性时,barStyle将失效

[self.searchBar setTranslucent:YES];// 设置是否透明

[self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];// 设置背景图片

[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"]forState:UIControlStateNormal];// 设置搜索框中文本框的背景

[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"]forState:UIControlStateHighlighted];

[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30,30)];// 设置搜索框中文本框的背景的偏移量

[self.searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中

[self.searchBar setShowsSearchResultsButton:YES];// 是否显示搜索结果按钮

[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 设置搜索框中文本框的文本偏移量

[self.searchBar setInputAccessoryView:_btnHide];// 提供一个遮盖视图

[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式

// 设置搜索框下边的分栏条

[self.searchBar setShowsScopeBar:YES];// 是否显示分栏条

[self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分栏条,栏目

[self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];// 分栏条的背景颜色

[self.searchBar setSelectedScopeButtonIndex:1];// 分栏条默认选中的按钮的下标

[self.searchBar setShowsBookmarkButton:YES];// 是否显示右侧的“书图标”

[self.searchBar setShowsCancelButton:YES];// 是否显示取消按钮

[self.searchBar setShowsCancelButton:YES animated:YES];

// 是否提供自动修正功能(这个方法一般都不用的)

[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 设置自动检查的类型

[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault

self.searchBar.delegate = self;// 设置代理

[self.searchBar sizeToFit];

myTableView.contentInset =UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);

[self.view addSubview:myTableView];

[myTableView addSubview:self.searchBar];

这么多属性,其实看起来多,你实际去操作事件一下,就发现很简单的!

绝大多部分都是定义一些外观的东西!了解了各个属性,一定能满足你设计出你想要的外观效果!!

然后,解释一下,我个人觉的比较有趣和重要的属性!

1.@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;属性

例如:

[self.searchBar setInputAccessoryView:your_View];// 提供一个遮盖视图

当处于UISearchBar焦点状态下(输入框正要输入内容时),会有一个遮盖视图。

你翻看一下,iPhone手机上的电话本搜索功能。那个遮盖视图就是一个半透明的黑色View。

查看了一下API,是iOS 6.0 以及以后,新加入的!

那么就意味这 iOS 6.0 之前的系统是不兼容的。那么怎么才能达到这个类似的效果呢?

变通一下,其实,很简单:仍然设置一个按钮,初始状态下,该UIButton控件透明度设置为0;并且在控件取得焦点时,设置透明度为1。

小技巧:如果要设置这个属性,那么,就最好定义一个UIButton控件,这样,当点击该遮盖层的话,可以利用按钮事件,

设置:[self.searchBar resignFirstResponder];让搜索框放弃第一焦点。(iPhone电话薄也是这么做的,感觉很人性化)。

迷惑:还有一个小的问题:当我让UISearchBar显示取消按钮时,当我让UISearchBar失去焦点时,我的取消按钮也不能点击了。衰啊。

看了一下iPhone电话薄的UISearchBar,竟然可以也,找了很久,都不知道是怎么回事,大概苹果又开始玩私有API了吧。

解决方法:很暴力,但是很好用!在UISearchBar上原来取消按钮的位置上覆盖一个UIButton,设置成一样的。呵呵。可以了。

类似如下:

// 遮盖层

_btnAccessoryView=[[UIButton alloc] initWithFrame:CGRectMake(0, 44, BOUNDS_WIDTH,BOUNDS_HEIGHT)];

[_btnAccessoryView setBackgroundColor:[UIColor blackColor]];

[_btnAccessoryView setAlpha:0.0f];

[_btnAccessoryView addTarget:self action:@selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_btnAccessoryView];

// 遮罩层(按钮)-点击处理事件

- (void) ClickControlAction:(id)sender{

NSLog(@"handleTaps");

[self controlAccessoryView:0];

}

// 控制遮罩层的透明度

- (void)controlAccessoryView:(float)alphaValue{

[UIView animateWithDuration:0.2 animations:^{

//动画代码

[self.btnAccessoryView setAlpha:alphaValue];

}completion:^(BOOL finished){

if (alphaValue<=0) {

[self.searchBar resignFirstResponder];

[self.searchBar setShowsCancelButton:NO animated:YES];

[self.navigationController setNavigationBarHidden:NO animated:YES];

}

}];

}

2.@property(nonatomic,assign) id<</b>UISearchBarDelegate> delegate;属性

例如:

self.searchBar.delegate = self;

说到这个属性,就是设置委托了。

UISearchBarDelegate委托定义了很多关于,搜索框的一些操作数据的协议方法!

先来个,特写,把x协议的家庭成员列出来:

@protocol UISearchBarDelegate

@optional

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;

@end

这不需要解释吧,看方法名称就能了解!

我们来看一看,常用的委托方法吧。

#pragma mark - UISearchBarDelegate 协议

// UISearchBar得到焦点并开始编辑时,执行该方法

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{

[self.searchBar setShowsCancelButton:YES animated:YES];

[self.navigationController setNavigationBarHidden:YES animated:YES];

[self controlAccessoryView:0.9];// 显示遮盖层。

return YES;

}

// 取消按钮被按下时,执行的方法

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

[self.searchBar resignFirstResponder];

[self.searchBar setShowsCancelButton:NO animated:YES];

[liveViewAreaTable searchDataBySearchString:nil];// 搜索tableView数据

[self.navigationController setNavigationBarHidden:NO animated:YES];

[self controlAccessoryView:0];// 隐藏遮盖层。

}

// 键盘中,搜索按钮被按下,执行的方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

NSLog(@"---%@",searchBar.text);

[self.searchBar resignFirstResponder];// 放弃第一响应者

[liveViewAreaTable searchDataBySearchString:searchBar.text];

[self.navigationController setNavigationBarHidden:NO animated:YES];

[self controlAccessoryView:0];// 隐藏遮盖层。

}

// 当搜索内容变化时,执行该方法。很有用,可以实现时实搜索

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{

NSLog(@"textDidChange---%@",searchBar.text);

[liveViewAreaTable searchDataBySearchString:searchBar.text];// 搜索tableView数据

[self controlAccessoryView:0];// 隐藏遮盖层。

}

3.遍历UISearchBar控件的子控件,这样可以针对不同的子视图来设置外观了。

for(id subView in [self.searchBar subviews]){

if([subView isKindOfClass:[UIButton class]]){

UIButton *btn = (UIButton *)subView;

[btn setTitle:@"取消"  forState:UIControlStateNormal];

}

}

UISearchBar控件的更多相关文章

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

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

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

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

  3. searchBar控件

    那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你意想不到的功能和特点! 首先,还是来普及一下UISearchBar控件API ...

  4. iOS开发--UIKit控件之UISearchBar(搜索栏)

    今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...

  5. 【iOS UISearchBar父控件是UIScrollView时,上移的问题】

    如果UISearchViewController的父控件是UIScrollView,点击UISearchBar后,它会移出控制器外.如下,使用UIScrollView作为"消息"和 ...

  6. iOS:搜索栏控件UISearchBar and SearchDisplayController的使用

    UISearchBar and SearchDisplayController控件: 这是一个带搜索栏和搜索显示控制器的控件,前面的SearchBar是一个搜索栏,它提供一个输入搜索条件的类似于文本框 ...

  7. iOS-UI-UI控件概述

    以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...

  8. [转]设置控件全局显示样式appearance proxy

    转自:huifeidexin_1的专栏 appearance是apple在iOS5.0上加的一个协议,它让程序员可以很轻松地改变某控件的全局样式(背景) @selector(appearance) 支 ...

  9. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

随机推荐

  1. cocos2d基础入门

    HelloCpp中Classes目录下放开发者自己的类: win32:平台相关,coco2d已默认创建:coco2d-x目录下,samples/cpp/HelloCpp/(工程根目录)图片放置位置:根 ...

  2. 正则RegEXp

    JavaScript RegExp 对象 RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/attributes 创建 RegE ...

  3. 修改SQL Server登录密码(使用SQL Server身份登录)

    修改登录密码: http://blog.sina.com.cn/s/blog_631611220100iqao.html

  4. FreeCodecamp:Repeat a string repeat a string

    要求: 重要的事情说3遍! 重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. 结果: repeat("*", 3) 应该返回"***". ...

  5. REF CURSOR和CURSOR

    REF CURSOR DECLARE TYPE TY_EMP_CUR IS REF CURSOR; V_Emp_Cur TY_EMP_CUR; V_Id EMP.ID%TYPE; BEGIN OPEN ...

  6. Python网络编程——修改套接字发送和接收的缓冲区大小

    很多情况下,默认的套接字缓冲区大小可能不够用.此时,可以将默认的套接字缓冲区大小改成一个更合适的值. 1. 代码 # ! /usr/bin/env python # -*- coding: utf-8 ...

  7. (zz)Linux下Gcc生成和使用静态库和动态库详解

    http://blog.chinaunix.net/uid-23592843-id-223539.html

  8. 我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)

    os模块 提供对操作系统进行调用的接口 >>> import os >>> os.getcwd() # 获取当前工作目录,类似linux的pwd命令 '/data/ ...

  9. 深入select_related与prefetch_related函数

    阅读博客http://blog.jobbole.com/74881/的笔记 在数据库有外键的时候,使用select_related()和prefetch_related()可以很好的减少数据库请求的次 ...

  10. perl 自动发产品

    use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...