好用的有多种样式的搜索界面创建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;

就可以改成圆形了。效果:


圆形的 UISearchBar

用这个方法还可以改取消按钮的颜色、字体什么的。

详细内容可以参考我的Demo代码,如果有问题可以留言: http://download.csdn.net/detail/hbblzjy/9676026

界面效果展示:

热门搜索样式:

           

历史记录样式:

           



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值