Giter Club home page Giter Club logo

pysearch's Introduction



Apps Using Total Downloads
Build Status Pod Version Pod Platform Pod License

  • 🔍 An elegant search controller for iOS.

QQ chat room

 

Features

  • Support a variety of hot search style
  • Support a variety of search history style
  • Support a variety of search results display mode
  • Support a variety of search view controller display mode
  • Support search suggestions
  • Support search history (record) cache
  • Support callback using delegate or block completion search
  • Support CocoaPods
  • Support localization
  • Support vertical and horizontal screen on iPhone and iPad

Requirements

  • iOS 7.0 or later
  • Xcode 7.0 or later

Architecture

Main

  • PYSearch
  • PYSearchConst
  • PYSearchViewController
  • PYSearchSuggestionViewController

Category

  • UIColor+PYSearchExtension
  • UIView+PYSearchExtension
  • NSBundle+PYSearchExtension

Contents

Renderings

Styles

Hot search style



Search history style



How to use

  • Use CocoaPods:
    • pod "PYSearch"
    • Import the main file:#import <PYSearch.h>
  • Manual import:
    • Drag All files in the PYSearch folder to project
    • Import the main file:#import "PYSearch.h"

Details (See the example program PYSearchExample for details)

    // 1. Create hotSearches array
    NSArray *hotSeaches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"];
    // 2. Create searchViewController
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"Search programming language" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // Call this Block when completion search automatically
        // Such as: Push to a view controller
        [searchViewController.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
        
    }];
    // 3. present the searchViewController
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];

Custom

  • Custom search suggestions display
// 1. Set dataSource
searchViewController.dataSource = self;
// 2. Implement dataSource method
  • Custom search result dispaly
// 1. Set searchResultShowMode
searchViewController.searchResultShowMode = PYSearchResultShowModeEmbed;
// 2. Set searchResultController 
searchViewController.searchResultController = [[UIViewController alloc] init];
  • Set hotSearchStyle(default is PYHotSearchStyleNormalTag)
// Set hotSearchStyle
searchViewController.hotSearchStyle = PYHotSearchStyleColorfulTag;
  • Set searchHistoryStyle(default is PYSearchHistoryStyleCell)
// Set searchHistoryStyle
searchViewController.searchHistoryStyle = PYSearchHistoryStyleBorderTag;
  • Set searchHistoriesCachePath(default is PYSEARCH_SEARCH_HISTORY_CACHE_PATH)
// Set searchHistoriesCachePath
searchViewController.searchHistoriesCachePath = @"The cache path";
  • Set searchHistoriesCount(default is 20)
// Set searchHistoriesCount
searchViewController. searchHistoriesCount = 6;
  • Set searchResultShowMode(default is PYSearchResultShowModeCustom)
// Set searchResultShowMode
searchViewController.searchResultShowMode = PYSearchResultShowModeEmbed;
  • Set searchSuggestionHidden(deafult is NO)
// Set searchSuggestionHidden
searchViewController.searchSuggestionHidden = YES;

Hope

  • If you have any questions during the process or want more interfaces to customize,you can issues me!
  • Instead of giving me star, it is better to throw a bug to me!
  • If you want to participate in the maintenance of this project or have a good design style, welcome to pull request!
  • If you feel slightly discomfort in use, please contact me QQ:499491531 or Email:[email protected].
  • Hope to improve this project together, let it become more powerful, able to meet the needs of most users!

Licenses

All source code is licensed under the MIT License.

pysearch's People

Contributors

anharismail avatar baocang avatar codejcson avatar ko1o avatar liuliangju avatar lzzhoujielun avatar sam-spencer avatar sunleic avatar xiao99xiao avatar zhongdom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pysearch's Issues

iOS7未适配

[UIImage imageNamed: inBundle: compatibleWithTraitCollection:]
方法iOS8+ only

代理

没有点击“取消”时的代理返回

默认样式问题

2

标题文字和清空按钮水平没有对齐 还有顶部的搜索框好像也有点问题 10.1的iPhone 7 模拟器

布局问题

1, 搜索历史为空时, 2,搜索历史为默认时

点击取消时,可以改变 dismiss 的动画吗

一般情况下,在进入和退出一个页面的时候,都会设置动画,不然会显得比较突兀。我看了 PYSearch 对点击取消的处理,是直接设定好了 dismiss 的方式,是没有动画的,我建议让增加参数来设置 dismiss 的方式(包括是否有动画,以及动画的方式),这样可能会更灵活。

/** 点击取消 */
- (void)cancelDidClick
{
    [self.searchBar resignFirstResponder];
    
    // dismiss ViewController
    [self dismissViewControllerAnimated:NO completion:nil];
    
    // 调用代理方法
    if ([self.delegate respondsToSelector:@selector(didClickCancel:)]) {
        [self.delegate didClickCancel:self];
    }
}

热门搜索和历史搜索的顺序能不能换

能提供一个接口,修改热门搜索和历史搜索的顺序. 而且有热门搜索的情况下,一般需求都是没搜索到内容,会在tableViewHeader显示一张图片,tableView显示热门搜索的内容! 可以考虑加上这个功能吗

PYSearchViewController 页面的 contentSize 比实际高度少 64

在集成 PYSearchViewController 后,当历史记录比较多需要滑动时,发现滑动不到底部,contentSize 的高度比实际少了一个导航栏的高度,应该是用户设置了 navigationBar.translucent 为 NO 后导致的,如果改为 YES 后这个问题就消失了。我有一个想法可以解决这类问题:通过在 PYSearchViewController 中检测用户是否有改变 translucent 的值,可以根据具体情况来设置 contentSize 的高度。

创建控制器的时候会提示无法响应方法

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0x7fca3bdd3970'
不知道是什么和它内部文件起了冲突

键盘问题

点击标签进入搜索结果界面,右滑返回时取消,再右滑返回,会有键盘显示,在viewWillAppear里面直接[self.searchBar becomeFirstResponder];没有做处理

关于初始化不传hotSeachs数组的问题

有一种使用场景,跳转到搜索界面的时候,热门搜索词是从网络请求过来的,初始化时为nil,后续网络返回时再赋值,这种情况下,热门搜索所有的Label不可见,frame计算有预留Label的位置

PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:nil searchBarPlaceholder:@"输入商品/店铺"];
    searchViewController.hotSearchStyle = PYHotSearchStyleBorderTag; // 热门搜索风格根据选择
    searchViewController.searchHistoryStyle = PYHotSearchStyleDefault; // 搜索历史风格为default
    //设置代理
    searchViewController.delegate = self;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                searchViewController.hotSearches = @[@"化妆棉",@"口红",@"鞋子",@"化妆棉",@"口红",@"鞋子",@"化妆棉",@"口红",@"鞋子",@"化妆棉",@"鞋子"];
            });
        });
    });
    
    //跳转到搜索控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];

关于"预留接口"和"用户自定义"等建议

少年有为的作者, 你好! 最近我在使用你写的这个框架, 感觉非常棒, 功能封装的很好, 很多时候只需要几行代码就能完成一个大功能. 另外, 我在使用的过程中也有一些心得和痛点, 算是抛砖引玉, 献丑了🙈

  1. 关于命名,
    我们先聊聊 iOS SDK 中 UISearchController 的一个初始化方法, - (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController;这其中涉及两个 controller, searchController 和 searchResultsController, 一个是搜索的主页面, 另一个是搜索的结果页面.

在 PYSearch 中, 对应功能的类分别为 PYSearchViewController 和 PYSearchSuggestionViewController, 此外, 在 PYSearchViewController 中还有一个属性 searchResultController, 三者的分别代表"搜索的主页面", "搜索的结果页面", "某条搜索结果的详情页",

这个时候用过 UISearchController 的用户可能就会迷惑---searchResultController 到底是搜索的结果页面 还是 某个搜索结果的详情页? PYSearchSuggestionViewController 和 searchResultController 有什么关系? 我在用的时候就产生了此类疑惑, 看了源码之后才明白 PYSearchSuggestionViewController 和 UISearchController 中的 searchResultsController 是一个意思. 我个人认为一个好的设计, 是不需要用户关心接口的实现的, 能见名知意.

我建议这种情况下类或属性的命名和原生 API 保持类似的风格, PYSearchSuggestionViewController 可以改为 PYSearchResultsViewController 或 PYSearchResultsController; 而"某一条搜索结果的详情页" iOS 的 SDK 没有涉及, 这一步留给了用户自己在 searchResultsController 中处理. PYSearch 中对应的这个属性 searchResultController 可以改名为 resultDetailController.

  1. 关于"自定义"的痛点
    这条内容又和 PYSearchResultsViewController 有关, 还得聊到 iOS SDK 中的 - (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController;. 在这个方法中, 搜索页面关联的 searchResultsController 是 UIViewController 类型的, 所以搜索结果页可以是单个 UITableView 页面, 也可以是复杂一点的一个 ScrollView 中嵌套的多个 tableView (比如"支付宝"的搜索页面), 总而言之, 这是一个用户可自定义的页面, 而不是局限为一个 tableView. 所以我建议搜索结果页的功能可以这样改: 搜索结果页的类型默认是UIViewController, 如果用户没有特别指定, 则只包含单个 tableView; 如果用户实现了自己的 searchResultsController, 则使用用户自定义的 controller 作结果页.

  2. 是一个预留控件的问题, 比如在 searchBar 的左边, 有时需要做一个可点击的下拉分类选择控件(参考淘宝的搜索页), searchBar 的右边, 有时需要显示一个语音输入按钮,根据用户的设置来决定是否显示, 不知道 PYSearch 目前也没有实现这个功能?

Displayed texts are not localized

Displayed texts are not localized :

  • PYSearchConst.m
  • PYSearchViewController.m lines 141 and 236

For example NSLocalizedString or accessibles properties.

Thanks !

添加移除ChildViewController方法 会比较好点

在- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 代理中判断

if (<#是否隐藏属性#> && searchText.length == 0 && self.searchResultShowMode == PYSearchResultShowModeEmbed) {
        [self.searchResultController willMoveToParentViewController:nil];
        [self.searchResultController.tableView removeFromSuperview];
        [self.searchResultController removeFromParentViewController];
    }

添加个属性 在输入框中没有字符的时候 可选择性的 隐藏结果的ViewController, 这样显示的还是baseViewController, 只是建议....

搜索历史记录无法覆盖

博主,我在使用你的demo的时候我只想使用你那个热门彩色有序排序功能的样式。可是我在拉取代码的时候其它的都拉取的差不多了,可是就差一个搜索历史的header地方不理想,对于header的地方总是有两处显示,下面三处截图就是我的拉取的代码和
img_0029
24c035dd-a99b-412b-abdc-1c93a0dbf73a
a7d4a958-0bcb-473a-ab14-89375cabe0f9

搜索结果显示

如果我想在搜索主界面当textDidChange时显示搜索结果列表,而不是要push到下个界面,能否对外提供一个tableView

布局问题

首先:很好的demo
我的工程里设置了navigationVC的两个属性;
问题1:搜索框里看不到光标;
问题儿:搜索建议页面会向下偏移
下面是我设置的navigationVC的属性,当我代码里注释这两行就不会有上面的两个问题.
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageName:@"someColorfulImage"] forBarMetrics:UIBarMetricsDefault];

and PYSearchViewController.m line 120 ,why set tableview.contentInset top 30?

初始化方法的返回值建议设置为 instancetype

在使用第三方框架时, 用户一般会继承框架中主要的类, 进行一层简单的封装, 便于代码解耦和框架的未来的更换(可能), 所以子类常需要使用父类的初始化方法. 此时子类在初始化时希望得到的是子类实例, 而不是父类实例, 所以在 PYSearchViewController 的初始化方法中, 返回值类型用 instancetype 比较好, 子类可以初始化出该子类的实例.
所以以下源码有三个地方可以优化:

  1. 两个方法的返回值都改为 instancetype;
  2. PYSearchViewController *searchVC = [[PYSearchViewController alloc] init]; 改为 PYSearchViewController *searchVC = [[self alloc] init];

源码

+ (PYSearchViewController *)searchViewControllerWithHotSearches:(NSArray<NSString *> *)hotSearches searchBarPlaceholder:(NSString *)placeholder
{
    PYSearchViewController *searchVC = [[PYSearchViewController alloc] init];
    searchVC.hotSearches = hotSearches;
    searchVC.searchBar.placeholder = placeholder;
    return searchVC;
}

+ (PYSearchViewController *)searchViewControllerWithHotSearches:(NSArray<NSString *> *)hotSearches searchBarPlaceholder:(NSString *)placeholder didSearchBlock:(PYDidSearchBlock)block
{
    PYSearchViewController *searchVC = [self searchViewControllerWithHotSearches:hotSearches searchBarPlaceholder:placeholder];
    searchVC.didSearchBlock = [block copy];
    return searchVC;
}

"热门搜索"与"搜索记录"挤在一起

你好,我想请问下,设置标题的时候"热门搜索"与"搜索记录"挤在一起了

 let searchVC: PYSearchViewController = PYSearchViewController.init(hotSearches: ["小飞侠", "佩佩猪"], searchBarPlaceholder: "请输入需要搜索的关键词") { (searchViewController, searchBar, searchString) in
            debugPrint("开始搜索")
        }
        searchVC.delegate = self
        searchVC.hotSearchHeader.text = "热门搜索"
        searchVC.hotSearchStyle = .colorfulTag
        searchVC.showSearchHistory = true
        searchVC.searchHistoryHeader.text = "搜索记录"
        searchVC.searchHistoryStyle = .cell
        self.present(vc: searchVC)

29cbeb3f-0a2c-4e73-a7fd-7ea2aeeb8992

热门搜索添加不到搜索历史中

热门搜索跳转页面与直接搜索跳转的页面不同,分别实现了代理方法(didSelectHotSearchAtIndex: 与 didSearchWithsearchBar: ),但是在搜索历史中找不到热门搜索词,只能直接搜索时才会出现,请问怎么解决?

热门搜索为空

image

如图,如果热门搜索为空时,显示历史搜索记录会发生位置偏移

存储的位置

注意到plist的保存路径 是NSDocumentDirectory 文件夹 ,能否自定义存储位置?

UISearchResultsUpdating 协议

请问 UISearchController 中的 UISearchResultsUpdating 协议如何在 PYSearch 中实现的?我在源码里没有找到。

两个问题

1.关键字和放大镜🔍重叠一起。demo没有问题,运行到项目里就有问题。
2.作者能把自动横屏关掉吗。
3.PYSearchResultShowModeEmbed模式下,我的搜索数据怎么传。

初始化时hotSearches为nil,稍后设置导致hotSearchStyle失效

#warning 初始化的时候hotSearches为nil
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:nil searchBarPlaceholder:@"搜索编程语言" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // 开始搜索执行以下代码
        // 如:跳转到指定控制器
        [searchViewController.navigationController pushViewController:[[PYTempViewController alloc] init] animated:YES];
    }];
    
    searchViewController.view.backgroundColor = [UIColor whiteColor];
    
    // 3. 设置风格
    if (indexPath.section == 0) { // 选择热门搜索
        searchViewController.hotSearchStyle = (NSInteger)indexPath.row; // 热门搜索风格根据选择
        searchViewController.searchHistoryStyle = PYHotSearchStyleDefault; // 搜索历史风格为default
    } else { // 选择搜索历史
        searchViewController.hotSearchStyle = PYHotSearchStyleDefault; // 热门搜索风格为默认
        searchViewController.searchHistoryStyle = (NSInteger)indexPath.row; // 搜索历史风格根据选择
    }
    // 4. 设置代理
    searchViewController.delegate = self;

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                searchViewController.hotSearches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"];
#warning 如果没有这句调用,标签的style就会变成默认值
//                searchViewController.hotSearchStyle = PYHotSearchStyleBorderTag;
            });
        });
    });
    
    
    // 5. 跳转到搜索控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.