uisearchbar修改高度 高度怎么改变

当前位置:
& Swift - 搜索条(UISearchBar)的用法
Swift - 搜索条(UISearchBar)的用法
发布:hangge
阅读:9932
1,搜索条Options属性还可设置如下功能样式:
Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件。
Shows Bookmarks Button:勾选后,搜索框右边会显示一个书本的按钮,单击会发送特殊事件。
Shows Cancel Button:勾选后,搜索框右边会出现一个“Cancel”按钮,单击会发送特殊事件。
Shows Scope Bar:勾选后,会在搜索条下面出现一个分段控制器。
2,下面是一个搜索条的使用样例,功能如下:
(1)在Main.storyboard界面里拖入一个 Search Bar 和一个 Table View,Search Bar放到Table View的页眉位置
(2)初始化或者搜索条为空时,表格显示所有数据
(3)搜索条不为空时,表格实时过滤显示匹配的项目
&&& && && &
4,代码如下
import UIKit
class ViewController: UIViewController,UISearchBarDelegate,
UITableViewDataSource,UITableViewDelegate {
// 引用通过storyboard创建的控件
@IBOutlet var searchBar : UISearchBar!
@IBOutlet var tableView : UITableView!
// 所有组件
var ctrls:[String] = ["Label","Button1","Button2","Switch"]
// 搜索匹配的结果,Table View使用这个数组作为datasource
var ctrlsel:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// 起始加载全部内容
self.ctrlsel = self.ctrls
//设置代理
self.searchBar.delegate = self
self.tableView.delegate = self
self.tableView.dataSource = self
// 注册TableViewCell
self.tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
// 返回表格行数(也就是返回控件数)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -& Int {
return self.ctrlsel.count
// 创建各单元显示内容(创建参数indexPath指定的单元)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-& UITableViewCell
// 为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = "SwiftCell"
// 同一形式的单元格重复使用,在声明时已注册
let cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = self.ctrlsel[indexPath.row]
return cell
// 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print(searchText)
// 没有搜索内容时显示全部组件
if searchText == "" {
self.ctrlsel = self.ctrls
else { // 匹配用户输入内容的前缀(不区分大小写)
self.ctrlsel = []
for ctrl in self.ctrls {
if ctrl.lowercaseString.hasPrefix(searchText.lowercaseString) {
self.ctrlsel.append(ctrl)
// 刷新Table View显示
self.tableView.reloadData()
// 搜索代理UISearchBarDelegate方法,点击虚拟键盘上的Search按钮时触发
/**func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作。iOS开发-搜索栏UISearchBar和UISearchController - 推酷
iOS开发-搜索栏UISearchBar和UISearchController
搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是不一样的。iOS中的搜索栏实现起来相对简单一点,网上也有很多参考资料,不过靠谱的不是很多,很多都是iOS 8.0之前的实现,iOS 8.0上的实现貌似很少看到,看了一些老外的代码,使用了一下UISearchController感觉还是非常不错的。
UISearchBar和UIDisplayController
是网上最常见的也算是最简单的,也有使用Searh Bar Search Display Controller的控件的,本文就简单的使用Search Bar和UITableView实现搜索Demo的,最上面的就是搜索栏,之前的就是TableView:
为了实现搜索需要声明委托
UISearchBarDelegate
, UISearchDisplayDelegate,其中搜索主要使用的就是UISearchDisplayDelegate,具体代码实现过程:
@property (strong,nonatomic) NSMutableArray
@property (strong,nonatomic) NSMutableArray
初始化数据:
self.dataList=[NSMutableArray arrayWithCapacity:100];
for (NSInteger i=0; i&100; i++) {
[self.dataList addObject:[NSString stringWithFormat:@&%ld-FlyElephant&,(long)i]];
//设置区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
设置区域的行数(重点),这个就是使用委托之后需要需要判断是一下是否是需要使用Search之后的视图:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchList count];
return [self.dataList count];
同样的返回单元格也有两种情况,一种是初始化数据,一种是过滤之后的数据视图:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@&cellFlag&;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
if (tableView==self.searchDisplayController.searchResultsTableView) {
[cell.textLabel setText:self.searchList[indexPath.row]];
[cell.textLabel setText:self.dataList[indexPath.row]];
UISearchBarDelegate中德 开始和结束的事件:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
NSLog(@&搜索Begin&);
return YES;
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
NSLog(@&搜索End&);
return YES;
搜索时过滤数据:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
// 谓词的包含语法,之前文章介绍过/xiaofeixiang/
NSPredicate *preicate = [NSPredicate predicateWithFormat:@&SELF CONTAINS[c] %@&, searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
//过滤数据
self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//刷新表格
return YES;
最终效果如下:
UISearchController实现搜索
UISeachBar通过UISearchDisplayDelegate实现上面的效果是没有问题的,网上也有很多类似的实现效果,不过是警告的,信息如下: 'searchDisplayController' is deprecated: first deprecated in iOS 8.0,这么明显一个警告总不能视而不见吧 ,
StackOverFlow
UISearchDisplayController is deprecated
, and recommended to use UISearchController instead
,也就是说
UISearchDisplayController,
也就是不推荐使用
UISearchDisplayDelegate
,但是可以通过
UISearchController
UISearchResultsUpdating 这个委托实现上面的效果;
视图中中需要声明UISearchResultsUpdating:
@interface ViewController : UITableViewController&UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating&
@property (nonatomic, strong) UISearchController *searchC
需要自己初始化一下UISearchController:
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater =
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchB
之前是通过判断搜索时候的TableView,不过现在直接使用self.searchController.active进行判断即可,也就是UISearchController的active属性:
//设置区域的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchList count];
return [self.dataList count];
//返回单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@&cellFlag&;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
[cell.textLabel setText:self.dataList[indexPath.row]];
具体调用的时候使用的方法也发生了改变,这个时候使用updateSearchResultsForSearchController进行结果过滤:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@&SELF CONTAINS[c] %@&, searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
//过滤数据
self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableView reloadData];
不过两者最终实现的效果的效果基本上是一致,殊途同归,本文难免有所遗漏,如有不当,请多多指正~
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 uisearchbar修改高度 的文章

 

随机推荐