项目作者: zonghongyan

项目描述 :
🔍Born for iOS 11 and iPhone X SearchBar
高级语言: Objective-C
项目地址: git://github.com/zonghongyan/EVNCustomSearchBar.git
创建时间: 2017-09-28T09:47:23Z
项目社区:https://github.com/zonghongyan/EVNCustomSearchBar

开源协议:MIT License

下载


EVNCustomSearchBar

🔍Born for iOS 11 and iPhone X SearchBar

Build Status
CocoaPods Compatible
License

预览图

other iPhone

iPhone

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

  1. $ gem install cocoapods

CocoaPods 1.1.0+ is required to build EVNCustomSearchBar.

To integrate EVNCustomSearchBar into your Xcode project using CocoaPods, specify it in your Podfile:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. platform :ios, '8.0'
  3. #use_frameworks!
  4. target '<Your Target Name>' do
  5. pod 'EVNCustomSearchBar', '~> 0.1.2'
  6. end

Then, run the following command:

  1. $ pod install

Use

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. [self initSearchBar];
  6. }
  7. - (void)didReceiveMemoryWarning {
  8. [super didReceiveMemoryWarning];
  9. // Dispose of any resources that can be recreated.
  10. }
  11. #pragma mark: 设置顶部导航搜索部分
  12. - (void)initSearchBar
  13. {
  14. self.navigationItem.titleView = self.searchBar;
  15. if (@available(iOS 11.0, *))
  16. {
  17. [self.searchBar.heightAnchor constraintLessThanOrEqualToConstant:kEVNScreenNavigationBarHeight].active = YES;
  18. }
  19. else
  20. {
  21. }
  22. }
  23. #pragma mark: getter method EVNCustomSearchBar
  24. - (EVNCustomSearchBar *)searchBar
  25. {
  26. if (!_searchBar)
  27. {
  28. _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, kEVNScreenStatusBarHeight, kEVNScreenWidth, kEVNScreenNavigationBarHeight)];
  29. _searchBar.backgroundColor = [UIColor clearColor]; // 清空searchBar的背景色
  30. _searchBar.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlack.png"];
  31. // _searchBar.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageTextColor.png"];
  32. _searchBar.iconAlign = EVNCustomSearchBarIconAlignCenter;
  33. [_searchBar setPlaceholder:@"请输入关键字"]; // 搜索框的占位符
  34. _searchBar.placeholderColor = TextGrayColor;
  35. _searchBar.delegate = self; // 设置代理
  36. [_searchBar sizeToFit];
  37. }
  38. return _searchBar;
  39. }
  40. #pragma mark: EVNCustomSearchBar delegate method
  41. - (BOOL)searchBarShouldBeginEditing:(EVNCustomSearchBar *)searchBar
  42. {
  43. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  44. return YES;
  45. }
  46. - (void)searchBarTextDidBeginEditing:(EVNCustomSearchBar *)searchBar
  47. {
  48. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  49. }
  50. - (BOOL)searchBarShouldEndEditing:(EVNCustomSearchBar *)searchBar
  51. {
  52. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  53. return YES;
  54. }
  55. - (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar
  56. {
  57. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  58. }
  59. - (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText
  60. {
  61. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  62. }
  63. - (BOOL)searchBar:(EVNCustomSearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  64. {
  65. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  66. return YES;
  67. }
  68. - (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar
  69. {
  70. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  71. }
  72. - (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar
  73. {
  74. NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
  75. }
  76. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  77. {
  78. [self.searchBar resignFirstResponder];
  79. }