Today, search controls are needed for demand reasons; This control has not been used before, so go to Baidu, find a can be said to be very complete information, thanks to the author. However, I have not found any properties or methods that can change the font size. Please let me know if you know. Thank you!

The original address: my.oschina.net/u/2340880/b…

Initialization: UISearchBar inherits from UIView, we can create searchBar just like we created View

UISearchBar *bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];
[self.view addSubview:bar];Copy the code

// This property sets the searchBar's search @property(nonatomic) UIBarStyle barStyle; // Enumerations are as follows: Typedef NS_ENUM(NSInteger, UIBarStyle) {UIBarStyleDefault = 0,// Default style white search box, extra background is gray UIBarStyleBlack = 1,// black style, Black search box // The following two enumerations have been disabled, Role and black style UIBarStyleBlackOpaque = 1, / / Deprecated. Use UIBarStyleBlack UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack andset the translucent property to YES
};Copy the code

Sets the text in the search box

@property(nonatomic,copy) NSString *text; Copy the code

The official explanation for this property is to display a line of text at the top of the search box, which is actually background text, as shown above:

@property(nonatomic,copy) NSString *prompt; Copy the code

bar.prompt = @"Search box";
bar.text=@"321111111111111111111111111"Copy the code

The effect is as follows:

Like the placeholder for other text input controls, it will disappear as text is entered

@property(nonatomic,copy) NSString *placeholder;  Copy the code

Whether to display a book button to the right of the search box, default is NO, YES effect is as follows:

@property(nonatomic) BOOL showsBookmarkButton; Copy the code

Whether to display the cancel button. The default value is NO. The effect of YES is as follows:

@property(nonatomic) BOOL showsCancelButton;Copy the code

Whether to display the search results button, the default is NO, YES effect is as follows:

@property(nonatomic) BOOL showsSearchResultsButton;Copy the code

Sets the check status of the search results button

@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected ;Copy the code

Set the display cancel button

- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated;Copy the code

Setting this color value affects the color of the cursor in the search box

@property(nonatomic,retain) UIColor *tintColor;Copy the code

Setting this color affects the background color of the search box

@property(nonatomic,retain) UIColor *barTintColor;Copy the code

Set the overall search box style

@property (nonatomic) UISearchBarStyle searchBarStyle;Copy the code

Enumerations are as follows:

typedef NS_ENUM(NSUInteger, UISearchBarStyle) { UISearchBarStyleDefault, // currently UISearchBarStyleProminent UISearchBarStyleProminent, } NS_ENUM_AVAILABLE_IOS(7_0);Copy the code

Set translucency

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;Copy the code

To display the search bar attachment select button view, first set this property to YES and then add buttons to the buttons array using the following property:

@property(nonatomic) BOOL showsScopeBar ;Copy the code

Sets the button title for the select button view

NSArray * scopeButtonTitles @ property (nonatomic, copy); @property(nonatomic) NSInteger selectedScopeButtonIndex;Copy the code

Set a default selected button

bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 200)];
bar.showsScopeBar=YES;
bar.scopeButtonTitles = @[@"12"The @"2"The @"3"The @"4"];Copy the code

Ancillary view of the keyboard

@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;Copy the code

Sets the background pattern for the search box

@property(nonatomic,retain) UIImage *backgroundImage;Copy the code

Sets the background pattern for the accessory select button view

@property(nonatomic,retain) UIImage *scopeBarBackgroundImage;Copy the code

This pair of methods sets and gets the background pattern for the search box under an enumeration of states

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;  - (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetricsCopy the code

This pair of methods is used to set and get the background pattern of the TextField in the search box

- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state; - (UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;Copy the code

This pair of methods is used to get and set the pattern of the search bar icon image

- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ; - (UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;Copy the code

This pair of methods is used to set and get the background pattern for the additional selection button view of the search box

- (void)setScopeBarButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state; - (UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state;Copy the code

This pair of methods is used to get and set the toggle button pattern in the additional selection button view

- (void)setScopeBarButtonDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState; - (UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;Copy the code

This pair of methods is used to set and get the font property dictionary for the toggle button title text

- (void)setScopeBarButtonTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state; - (NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state;Copy the code

Search text position offset in the search box

 @property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment;Copy the code

Textfield position offset in the search box

@property(nonatomic) UIOffset searchTextPositionAdjustment;Copy the code

Sets the position offset of the image in the search bar

- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon; - (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;Copy the code

The enumeration of pictures is as follows:

Typedef NS_ENUM(NSInteger, UISearchBarIcon) {UISearchBarIconSearch, // UISearchBarIconClear, / / remove icon UISearchBarIconBookmark, / / the book icon UISearchBarIconResultsList, / / the result list icon};Copy the code

Here are some proxy methods for the search box control:

The callback to begin editing, which returns NO and cannot be edited

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;  Copy the code

Callbacks have been started when editing

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;Copy the code

Callback to end editing

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;Copy the code

The edit callback is finished

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;Copy the code

Edit the text change callback

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;Copy the code

The callback before the edit text changed. Returning NO will not add the new edit text

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;Copy the code

Search button click callback

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;Copy the code

Book button click callback

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;Copy the code

Cancel the callback of button clicks

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;Copy the code

Search results button click callback

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;Copy the code

A callback triggered by the toggle button in the additional view of the search bar

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;Copy the code