原文:http://guohuaden.com/2017/05/20/GH-SearchView/
對(duì)于iOS自帶的搜索框立轧,其實(shí)我用著也挺好的,但低版本的使用: 高版本的使用:UISearchController,兩個(gè)難有個(gè)統(tǒng)一。很多項(xiàng)目中都是將著兩套代碼都寫入項(xiàng)目中去,這樣做難免顯得代碼冗余。所以就簡(jiǎn)單的自定義個(gè)搜索框赶熟。
下載地址:https://github.com/Wheat-Qin/GH_SearchView 【支持pods】
怎樣使用<code>'GH_SearchView':</code>
安裝CocoaPods:<code>pod 'GH_SearchView' </code>
手動(dòng)倒入:
拖動(dòng)<code>GH_SearchView</code>文件夾中的所有文件到工程項(xiàng)目中
引入主要文件:<code>#import "GH_SearchView.h"</code>
使用時(shí)實(shí)現(xiàn)起來也很簡(jiǎn)單,一句代碼創(chuàng)建:
GH_SearchView *searchView_1 = [[GH_SearchView alloc]initWithFrame:CGRectMake(10, 80, IPHONE_WIDTH-20, 44.f) withTarget:self backgroundColor:[UIColor redColor] placeholderContent:@"請(qǐng)輸入搜索內(nèi)容" searchImage:@"GH_search" withTableView:nil];
//自定制取消按鈕字體顏色
[searchView_1 setCancelButtonTitleColor:[UIColor blueColor]];
//設(shè)置背景色透明度
[searchView_1 setBackgroundColorWithAlphaComponent:0.5];
//設(shè)置激活狀態(tài)
[searchView_1 setSearchActiveState:YES];
[self.view addSubview:searchView_1];
//或者
//self.tableView.tableHeaderView = self.searchView;
使用時(shí)需遵守<code>GH_searchDelegate</code>協(xié)議陷嘴。
其中必需實(shí)現(xiàn)的協(xié)議方法有一種:
/**
搜索按鈕
@param content 搜索的內(nèi)容
*/
-(void)searchClickedWithContent:(NSString *)content;
可選的協(xié)議方法有三種:
/**
實(shí)時(shí)搜索協(xié)議方法
@param content 搜索的內(nèi)容
*/
-(void)searchBoxContentDidChange:(NSString *)content;
/**
搜索框開始輸入
@param content 搜索的內(nèi)容
*/
-(void)searchDidBeginEditing:(NSString *)content;
/**
搜索框輸入結(jié)束
@param content 搜索的內(nèi)容
*/
-(void)searchDidEndEditing:(NSString *)content;
效果圖如下:
無tableView情況下:
GH_searchView_1
有tableView情況下:
GH_searchView_2