All Products
Search
Document Center

Search bar component

Last Updated: May 25, 2021

AUSearchBar is a search bar control of mPaaS. It supports the following styles:

  • AUSearchBarStyleNormal: search bar with a Cancel button, for example, search bar on the homepage for global search.
  • AUSearchBarStyleDetail: search bar with a Cancel button and a back icon, for example, search bar on a level-2 page for global search。

Sample images

Dependency

The dependency of AUSearchBar is as follows:

 
  1. AntUI(iOS)
  2. 1.0.0.161108003457
  3. APCommonUI(iOS)
  4. 1.2.0.161108102201

API description

 
  1. @class AUSearchBar;
  2. @protocol AUSearchBarDelegate <NSObject>
  3. @optional
  4. #pragma mark - Proxy methods corresponding to UITextField.
  5. //
  6. - (BOOL)searchBarTextShouldBeginEditing:(AUSearchBar *)searchBar;
  7. //
  8. - (BOOL)searchBarTextShouldEndEditing:(AUSearchBar *)searchBar;
  9. // Called when text starts editing.
  10. - (void)searchBarTextDidBeginEditing:(AUSearchBar *)searchBar;
  11. // Called when text ends editing.
  12. - (void)searchBarTextDidEndEditing:(AUSearchBar *)searchBar;
  13. // Called when text changes (including clear).
  14. - (void)searchBar:(AUSearchBar *)searchBar textDidChange:(NSString *)searchText;
  15. // Called before text changes.
  16. - (BOOL)searchBar:(AUSearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
  17. - (BOOL)searchBarShouldClear:(AUSearchBar *)searchBar;
  18. #pragma mark - Other proxy methods.
  19. // Called when the search icon is clicked.
  20. - (void)searchBarSearchButtonClicked:(AUSearchBar *)searchBar;
  21. // Called when the Cancel button is clicked.
  22. - (void)searchBarCancelButtonClicked:(AUSearchBar *) searchBar;
  23. // Called when the back icon is clicked (valid for the AUSearchBarStyleDetail style).
  24. - (void)searchBarBackButtonClicked:(AUSearchBar *)searchBar;
  25. // Called when the voice icon is clicked (valid when shouldShowVoiceButton is set to YES).
  26. - (void)searchBarOpenVoiceAssister:(AUSearchBar *)searchBar;
  27. @end
  28. typedef NS_ENUM(NSUInteger, AUSearchBarStyle) {
  29. AUSearchBarStyleNormal = 0,//normal.
  30. AUSearchBarStyleDetail, //has back Button
  31. };
  32. /**
  33. The search bar control. (By default, the width is the same as that of the screen, and the height is 44.)
  34. */
  35. @interface AUSearchBar : UIView
  36. @property (nonatomic, strong) NSString *text; // The search box text.
  37. @property (nonatomic, assign) BOOL isSupportHanziMode; // Whether to support search while input. Default value: YES.
  38. @property (nonatomic, assign) AUSearchBarStyle style; // The style of the search box.
  39. @property (nonatomic, assign) BOOL shouldShowVoiceButton; // Whether to display the Voice button. Default value: NO.
  40. @property (nonatomic, strong, readonly) UITextField *searchTextField; // The search box.
  41. @property (nonatomic, weak) id<AUSearchBarDelegate> delegate;
  42. /**
  43. The initialization method.
  44. @param style The search bar style.
  45. @return Return an AUSearchBar instance.
  46. */
  47. - (instancetype)initWithStyle:(AUSearchBarStyle)style;
  48. @end

Sample code

  • Add to the navigation bar
 
  1. AUSearchBar *searchBar = [[AUSearchBar alloc] initWithStyle:AUSearchBarStyleNormal];
  2. searchBar.searchTextField.placeholder = @"搜索栏样式 (AUSearchBarStyleNormal)";
  3. searchBar.delegate = self;
  4. searchBar.isSupportHanziMode = YES;
  5. searchBar.shouldShowVoiceButton = YES;
  6. self.navigationItem.titleView = searchBar;
  7. self.navigationItem.leftBarButtonItem = nil; // Add no button to the left of the search bar.
  8. self.navigationItem.rightBarButtonItem = nil; // Add no button to the right of the search bar.
  9. self.navigationItem.hidesBackButton = YES; // Hide the back icon.
  • Add to a normal view
 
  1. searchBar = [[AUSearchBar alloc] initWithStyle:AUSearchBarStyleDetail];
  2. searchBar.searchTextField.placeholder = @"搜索栏样式 (AUSearchBarStyleDetail)";
  3. searchBar.delegate = self;
  4. searchBar.isSupportHanziMode = YES;
  5. searchBar.shouldShowVoiceButton = YES;
  6. [self.view addSubview:searchBar];