InfoSearchView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // InfoSearchView.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "InfoSearchView.h"
  9. @interface InfoSearchView () <UISearchBarDelegate>
  10. @property (nonatomic, strong) TDButton *backButton;
  11. @property (nonatomic, strong) TDButton *menuButton;
  12. @property (nonatomic, strong) TDButton *cancelButton;
  13. @property (nonatomic, strong) UIView *lineView;
  14. @end
  15. @implementation InfoSearchView
  16. - (instancetype)initWithFrame:(CGRect)frame type:(InfoSearchViewType)type
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. self.backButton.frame = CGRectMake(0, 0, 50,44);
  21. self.menuButton.frame = CGRectMake(50, 0, 35,44);
  22. self.searchBar.frame = CGRectMake(90, 4, kGXScreenWidth - 105, 36);
  23. self.menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  24. [self addSubview:self.backButton];
  25. [self addSubview:self.menuButton];
  26. [self addSubview:self.searchBar];
  27. // if (type == InfoSearchViewTypeCancel) {
  28. // CGFloat rightMargin = 16.f;
  29. //
  30. // CGSize btnSize = [self.cancelButton sizeThatFits:CGSizeZero];
  31. // self.cancelButton.frame = CGRectMake(kGXScreenWidth - rightMargin - btnSize.width, (frame.size.height-btnSize.height)/2.0, btnSize.width, btnSize.height);
  32. // [self addSubview:self.cancelButton];
  33. // rightMargin += btnSize.width + 16.f;
  34. //
  35. // self.searchBar.frame = CGRectMake(16+7+16, 7, kGXScreenWidth-32-7-rightMargin, 30);
  36. // }
  37. // [self.searchBar becomeFirstResponder];
  38. }
  39. return self;
  40. }
  41. - (void)setNowSearchData:(NSString *)searchStr {
  42. _searchBar.text = searchStr;
  43. }
  44. - (void)showLineView {
  45. [self addSubview:self.lineView];
  46. CGRect frame = self.frame;
  47. _lineView.frame = CGRectMake(0, frame.size.height - 1, kGXScreenWidth, 0.5);
  48. }
  49. - (void)backAction:(TDButton *)sender {
  50. if (self.backBlock) {
  51. self.backBlock();
  52. }
  53. }
  54. - (void)menuAction:(TDButton *)sender {
  55. if (self.menuBlock) {
  56. self.menuBlock();
  57. }
  58. }
  59. - (void)cancelAction:(TDButton *)sender {
  60. if (self.cancelBlock) {
  61. self.cancelBlock();
  62. }
  63. }
  64. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  65. if ([self.delegate respondsToSelector:@selector(textDidChange:)]) {
  66. [self.delegate textDidChange:searchText];
  67. }
  68. }
  69. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  70. if ([self.delegate respondsToSelector:@selector(searchButtonAction:)]) {
  71. [self.delegate searchButtonAction:searchBar];
  72. }
  73. }
  74. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  75. self.searchBar.placeholder = @"";
  76. return YES;
  77. }
  78. - (void)setBarPlaceholder:(NSString *)searchPlaceholder
  79. {
  80. self.searchBar.placeholder = searchPlaceholder;
  81. self.searchPlaceholder = searchPlaceholder;
  82. }
  83. - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
  84. if (self.searchPlaceholder.length > 0) {
  85. self.searchBar.placeholder = self.searchPlaceholder;
  86. }else{
  87. self.searchBar.placeholder = @" 搜索";
  88. }
  89. return YES;
  90. }
  91. #pragma mark - setter
  92. - (TDButton *)backButton {
  93. if (!_backButton) {
  94. _backButton = [TDButton buttonWithType:UIButtonTypeCustom];
  95. [_backButton setCurrentButtonHotSize:CGSizeZero];
  96. [_backButton setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  97. [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
  98. }
  99. return _backButton;
  100. }
  101. - (TDButton *)menuButton {
  102. if (!_menuButton) {
  103. _menuButton = [TDButton buttonWithType:UIButtonTypeCustom];
  104. [_menuButton setCurrentButtonHotSize:CGSizeZero];
  105. [_menuButton setImage:IMG(@"menu_black_icon") forState:UIControlStateNormal];
  106. [_menuButton addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
  107. }
  108. return _menuButton;
  109. }
  110. - (TDSearchBar *)searchBar {
  111. if (!_searchBar) {
  112. _searchBar = [[TDSearchBar alloc] initWithFrame:CGRectZero];
  113. // _searchBar.placeholder = @" 搜索";
  114. _searchBar.delegate = self;
  115. }
  116. return _searchBar;
  117. }
  118. - (TDButton *)cancelButton {
  119. if (!_cancelButton) {
  120. _cancelButton = [TDButton buttonWithType:UIButtonTypeCustom];
  121. [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  122. [_cancelButton setTitleColor:UIColorHex(1682DA) forState:UIControlStateNormal];
  123. [[_cancelButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  124. [_cancelButton setCurrentButtonHotSize:CGSizeZero];
  125. [_cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
  126. }
  127. return _cancelButton;
  128. }
  129. - (UIView *)lineView {
  130. if (!_lineView) {
  131. _lineView = [UIView new];
  132. _lineView.hidden = YES;
  133. _lineView.backgroundColor = UIColorHex(CCCCCC);
  134. }
  135. return _lineView;
  136. }
  137. @end