好用的有多种样式的搜索界面创建UISearchBar
之前看到一个别人封装的第三方PYSearch,相当于一个完整的搜索界面,今天在这里进行代码说明一下。
将PYSearch拖进项目或者使用Pods进行加库,我是直接拖进项目中进行使用
PYSearch库主要是PYSearch.h头文件,其中还有一些宏定义的头文件,界面设计,类别设计等头文件
项目引入头文件PYSearch.h
然后开始进行界面设计:
//创建搜索界面
//参数一:热门词语数组,参数二:搜索框的占位符,参数三:点击搜索以后执行block块
PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:_hotArray searchBarPlaceholder:@"搜索热门娱乐消息" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
// 开始搜索执行以下代码
// 如:跳转到指定控制器
[searchViewController.navigationController pushViewController:[[DetailViewController alloc] init] animated:YES];
}];
//设置风格
if (indexPath.section == 0)
{
//设置热门搜索风格
searchViewController.hotSearchStyle = (NSInteger)indexPath.row;
//设置历史记录风格,选择默认风格
searchViewController.searchHistoryStyle = PYHotSearchStyleDefault;
}
else
{
//设置热门搜索风格,选择默认风格
searchViewController.hotSearchStyle = PYHotSearchStyleDefault;
//设置历史记录风格
searchViewController.searchHistoryStyle = (NSInteger)indexPath.row;
}
//设置代理
searchViewController.delegate = self;
//这里显示搜索控制器,最好使用模态present形式,否则直接使用主界面的[self.navigationController pushViewController:searchViewController animated:YES];,会出现界面偏差问题,而且返回时会导致键盘消失不及时(即使不显示取消按钮也会导致键盘消失不及时的情况),以后使用可以根据需要修改代码
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[self presentViewController:nav animated:NO completion:nil];
//还有一种是创建没有取消按钮的类方法(+ (PYSearchViewController *)searchViewControllerWithHotSearches:(NSArray<NSString *> *)hotSearches searchBarPlaceholder:(NSString *)placeholder;),但是需要实现delegate方法:- (void)searchViewController:(PYSearchViewController *)searchViewController didSearchWithsearchBar:(UISearchBar *)searchBar searchText:(NSString *)searchText;
使用第三方创建搜索界面完成后,进行协议方法的调用
#pragma mark - PYSearchViewControllerDelegate
//搜索框文本变化时,显示的搜索建议通过searchViewController的searchSuggestions赋值即可
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
if (searchText.length) { // 与搜索条件再搜索
// 根据条件发送查询(这里模拟搜索)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 添加建议搜索结果
NSMutableArray *searchSuggestionsM = [NSMutableArray array];
for (int i = 0; i < arc4random_uniform(5) + 10; i++) {
NSString *searchSuggestion = [NSString stringWithFormat:@"搜索建议 %d", i];
[searchSuggestionsM addObject:searchSuggestion];
}
//显示输入搜索框文字的匹配结果
searchViewController.searchSuggestions = searchSuggestionsM;
});
}
}
调用第三方中的的样式类型:
typedef NS_ENUM(NSInteger, PYHotSearchStyle) { // 热门搜索标签风格
PYHotSearchStyleNormalTag, // 普通标签(不带边框)
PYHotSearchStyleColorfulTag, // 彩色标签(不带边框,背景色为随机彩色)
PYHotSearchStyleBorderTag, // 带有边框的标签,此时标签背景色为clearColor
PYHotSearchStyleARCBorderTag, // 带有圆弧边框的标签,此时标签背景色为clearColor
PYHotSearchStyleRankTag, // 带有排名标签
PYHotSearchStyleRectangleTag, // 矩形标签,此时标签背景色为clearColor
PYHotSearchStyleDefault = PYHotSearchStyleNormalTag // 默认为普通标签
};
typedef NS_ENUM(NSInteger, PYSearchHistoryStyle) { // 搜索历史风格
PYSearchHistoryStyleCell, // UITableViewCell 风格
PYSearchHistoryStyleNormalTag, // PYHotSearchStyleNormalTag 标签风格
PYSearchHistoryStyleColorfulTag, // 彩色标签(不带边框,背景色为随机彩色)
PYSearchHistoryStyleBorderTag, // 带有边框的标签,此时标签背景色为clearColor
PYSearchHistoryStyleARCBorderTag, // 带有圆弧边框的标签,此时标签背景色为clearColor
PYSearchHistoryStyleDefault = PYSearchHistoryStyleCell // 默认为 PYSearchHistoryStyleCell
};
typedef NS_ENUM(NSInteger, PYSearchResultShowMode) { // 搜索结果显示方式
PYSearchResultShowModeCustom, // 通过自定义显示
PYSearchResultShowModePush, // 通过Push控制器显示
PYSearchResultShowModeEmbed, // 通过内嵌控制器View显示
PYSearchResultShowModeDefault = PYSearchResultShowModeCustom // 默认为用户自定义(自己处理)
};
协议方法:
@protocol PYSearchViewControllerDelegate <NSObject, UITableViewDelegate>
@optional
/** 点击(开始)搜索时调用 */
- (void)searchViewController:(PYSearchViewController *)searchViewController didSearchWithsearchBar:(UISearchBar *)searchBar searchText:(NSString *)searchText;
/** 搜索框文本变化时,显示的搜索建议通过searchViewController的searchSuggestions赋值即可 */
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText;
/** 点击取消时调用 */
- (void)didClickCancel:(PYSearchViewController *)searchViewController;
@end
如果界面样式需要修改的可以看代码根据需要修改,比如,搜索框的样式界面的显示,等等问题
有些需求非要把 UISearchBar 默认的圆角矩形的圆角改大,顶端改成圆形的。虽然系统没有提供这个 API,不过还是有一个简单方法可以解决。
首先在 UIView 的 category 里加一个方法:
UIView+Utils.m
- (UIView*)subViewOfClassName:(NSString*)className {
for (UIView* subView in self.subviews) {
if ([NSStringFromClass(subView.class) isEqualToString:className]) {
return subView;
}
UIView* resultFound = [subView subViewOfClassName:className];
if (resultFound) {
return resultFound;
}
}
return nil;
}
用的时候:
UIView* backgroundView = [searchBar subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
backgroundView.layer.cornerRadius = 14.0f;
backgroundView.clipsToBounds = YES;
就可以改成圆形了。效果:

用这个方法还可以改取消按钮的颜色、字体什么的。
详细内容可以参考我的Demo代码,如果有问题可以留言:http://download.csdn.net/detail/hbblzjy/9676026
界面效果展示:
热门搜索样式:
历史记录样式:
好用的有多种样式的搜索界面创建UISearchBar的更多相关文章
- [Android源码]Android源码之高仿飞鸽传书WIFI热点搜索与创建(一)
(本文详情来源:android源码 http://www.eoeandroid.com/thread-296427-1-1.html 转载请注明出处!) [Android源码分享]飞鸽传书的An ...
- CRM WEB UI 03搜索界面新建按钮调到详细界面
这个和上一个差不多,简单说下: 1.因为NEW是在创建搜索界面的时候加的,所以此时只需在结果界面重定义NEW事件: method EH_ONNEW. OP_NEW( ). endmethod. 2.结 ...
- 如何利用Power BI 制作动态搜索界面
最近Power BI有了最新更新,想着利用 Power BI 工具制造一个动态的搜索界面,比如动态切换搜索引擎,分别从百度.360.搜狗等搜索苹果最新新闻.通过一番测试,最终实现了相关功能. 数据加载 ...
- Day12-微信小程序实战-交友小程序-优化“附近的人”页面与serach组件的布局和样式以及搜索历史记录和本地缓存*内附代码)
回顾/:我们已经实现了显示附近的人的功能了,可以多个人看到附近的人页面了 但是还是要进行优化有几个问题:1.我们用户选择了其他的自定义头像之后,在首页可以看到头像的变化,但是在附近的人中头像会变成报错 ...
- Android FragmentTransactionExtended:使Fragment以多种样式动画切换
有多种fragment之间切换的效果,效果是这样的: Demo的实现是很简单的. 在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用 ...
- AspNetPager实现真分页+多种样式
真假分页 分页是Web应用程序中最常用到的功能之一.当从数据库中获取的记录远远超过界面承载能力的时候,使用分页可以使我们的界面更加美观,更加的用户友好.分页包括两种类型:真分页和假分页. 其中假分页就 ...
- Jquery qTip2实现多种提示效果,支持ajax,以及多种样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (十九)TableView的点击监听和数据刷新(Alert的多种样式) -tag传值的技巧
要实现监听,要使用代理,控制器要成为TableView的代理. 注意下面的方式是代理方法: - (void)tableView:(UITableView *)tableView didSelectRo ...
- Android多种样式的进度条
原创 2016年04月26日 16:46:35 标签: android / clip / 进度条 / 8473 编辑 删除 ---- The mark of the immature man is t ...
随机推荐
- PostgreSQL的insert注入
写这篇文是在昨夜的ctf中遇到的. ctf地址:bloody-feedback.quals.2017.volgactf.ru email存在注入,在ctf中发现注入就很好办了,只要找到能绕过的方法就行 ...
- ftp爆破(python脚本)
最近在乌云看到一份端口详解:为了锻炼自己,按照端口详解写脚本 #!/usr/local/bin/ python #-*- coding: UTF-8 -*- __author__ = '' from ...
- [HNOI 2002]跳蚤
Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个 ...
- [UOJ UNR#1]奇怪的线段树
来自FallDream的博客,未经允许,请勿转载, 谢谢. 原题可以到UOJ看,传送门 如果存在一个点是白的,却有儿子是黑的,显然无解. 不然的话,只要所有黑色的“黑叶子”节点,即没有黑色的儿子的节点 ...
- VK Cup 2017 - Квалификация 1
CF上的VK Cup 2017资格赛1,好像很水,因为只有俄文所以语言是最大的障碍--不过之后正式赛貌似就有英文了.(比赛貌似只有开俄文模式才看的到--) 时长1天,不随时间扣分.FallDream ...
- [BZOJ]1023 cactus仙人掌图(SHOI2008)
NOIP后的第一次更新嗯. Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在 ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- QSDK下驱动AR8035
0 概述 QSDK平台中,我所接触到的版本,能支持MIPS架构的,是基于Openwrt AA版本:虽然CC版本上就已经能很好地支持AR8035了,可是AA版本它本身是不支持的,于是不断有人要求提供补丁 ...
- JS 实现点击页面任意位置隐藏div、span
通过调用下面的 showhidden(“标签ID”) 显示div/span/…等标签内容,可以实现点击页面任意地方再次隐藏该标签内容,而showhidden(“标签ID”,”nohidden”)可保存 ...
- Just for mysql
mysql的下载与安装 由于学校开设了数据库专业,并且最近准备在做一个web端的设计,虽然本人是负责前端(当然,前端技术也很LOW),但因种种原因,准备开始学习数据库相关的知识,以mysql为例. 昨 ...