UISearchBar 一般配合UITableView使用,当然也可以配合UITextView或者UIWebView使用。
先看他的几个属性:
@property(nonatomic) UIBarStyle barStyle; // default is UIBarStyleDefault (blue) @property(nonatomic,assign) id<UISearchBarDelegate> delegate; // weak reference. default is nil @property(nonatomic,copy) NSString *text; // current/starting search text @property(nonatomic,copy) NSString *prompt; // default is nil @property(nonatomic,copy) NSString *placeholder; // default is nil @property(nonatomic) BOOL showsBookmarkButton; // default is NO @property(nonatomic) BOOL showsCancelButton; // default is NO @property(nonatomic) BOOL showsSearchResultsButton __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2); // default is NO @property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2); // default is NO - (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); @property(nonatomic,retain) UIColor *tintColor; // default is nil @property(nonatomic,assign,getter=isTranslucent) BOOL translucent __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // Default is NO. Always YES if barStyle is set to UIBarStyleBlackTranslucent // available text input traits @property(nonatomic) UITextAutocapitalizationType autocapitalizationType; // default is UITextAutocapitalizationTypeNone @property(nonatomic) UITextAutocorrectionType autocorrectionType; // default is UITextAutocorrectionTypeDefault @property(nonatomic) UITextSpellCheckingType spellCheckingType; // default is UITextSpellCheckingTypeDefault @property(nonatomic) UIKeyboardType keyboardType; // default is UIKeyboardTypeDefault @property(nonatomic,copy) NSArray *scopeButtonTitles __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // array of NSStrings. no scope bar shown unless 2 or more items @property(nonatomic) NSInteger selectedScopeButtonIndex __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // index into array of scope button titles. default is 0. ignored if out of range @property(nonatomic) BOOL showsScopeBar __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is NO. if YES, shows the scope bar. call sizeToFit: to update frame // 1pt wide images and resizable images will be scaled or tiled according to the resizable area, otherwise the image will be tiled @property(nonatomic,retain) UIImage *backgroundImage __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR; @property(nonatomic,retain) UIImage *scopeBarBackgroundImage __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
@protocol UISearchBarDelegate <NSObject> @optional - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear) - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // called before text changes - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // called when bookmark button pressed - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar; // called when cancel button pressed - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2); // called when search results button pressed - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); @end