//UISearchBar.h
typedef NS_ENUM(NSInteger, UISearchBarIcon) {
UISearchBarIconSearch, //搜索放大镜的图标
UISearchBarIconClear, //取消的图标
UISearchBarIconBookmark, //书本图标
UISearchBarIconResultsList, // 搜索结果下拉图标
};
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // 搜索栏默认样式
UISearchBarStyleProminent, // 显示背景样式
UISearchBarStyleMinimal // 不显示背景样式
} NS_ENUM_AVAILABLE_IOS(7_0);
@protocolUISearchBarDelegate;
@class UITextField,UILabel,UIButton,UIColor;
NS_CLASS_AVAILABLE_IOS(2_0)@interface UISearchBar :UIView <UIBarPositioning,UITextInputTraits>
- (instancetype)init;//简单是初始化
- (instancetype)initWithFrame:(CGRect)frame;//指定搜索栏位置及大小
- (nullableinstancetype)initWithCoder:(NSCoder *)aDecoder; //未知
@property(nonatomic) UIBarStyle barStyle;//搜索栏样式默认值是UIBarStyleDefault
@property(nullable,nonatomic,weak)id<UISearchBarDelegate> delegate; //代理属性,默认值空
@property(nullable,nonatomic,copy) NSString *text; //给搜索栏指定值
@property(nullable,nonatomic,copy)NSString *prompt; //一直显示在搜索栏上面的提示值,不会消失的
@property(nullable,nonatomic,copy)NSString *placeholder;//提示值,输入值的时候会消失,没值的时候会自动出现
@property(nonatomic) BOOL showsBookmarkButton; //控制书本图标的显示,默认值NO
@property(nonatomic) BOOL showsCancelButton;//控制取消图标的显示,默认值NO
@property(nonatomic) BOOL showsSearchResultsButton //控制搜索结果下拉图标的显示,默认值NO
@property(nonatomic,getter=isSearchResultsButtonSelected)BOO searchResultsButtonSelected //控制
- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated;//设置取消按钮的显示以及动画
@property (nonatomic,readonly,strong)UITextInputAssistantItem *inputAssistantItem;
@property(null_resettable,nonatomic,strong)UIColor *tintColor; //设置光标的颜色(若显示取消该设置会一同改变取消的字体颜色)
@property(nullable,nonatomic,strong)UIColor *barTintColor;//搜索框外面的颜色
@property (nonatomic)UISearchBarStyle searchBarStyle;//搜索框的样式
@property(nonatomic,assign,getter=isTranslucent)BOO translucent; //设置半透明效果
//showsScopeBar,scopeButtonTitles,selectedScopeButtonIndex三个互相结合使用,只有让选择按钮显示了其它两个属性设置才有意义
@property(nullable,nonatomic,copy)NSArray<NSString *> *scopeButtonTitles;//设置范围按钮的标题,且只能放入NSString对象
@property(nonatomic)NSInteger selectedScopeButtonIndex; //设置范围按钮的默认选项,起始值默认值为0.
@property(nonatomic)BOOL showsScopeBar; //控制搜索按钮的显示,默认NO,设置为YES,searchBar调用sizeToFit:函数更新fame值
/* Allow placement of an input accessory view to the keyboard for the search bar
*/
@property (nullable,nonatomic,readwrite,strong)UIView *inputAccessoryView;
@property(nullable,nonatomic,strong)UIImage *backgroundImage; //设置背景图片
@property(nullable,nonatomic,strong)UIImage *scopeBarBackgroundImage;//设置范围按钮的背景图片
- (void)setBackgroundImage:(nullableUIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics; //设置背景图片
- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics;//获取背景图片
- (void)setSearchFieldBackgroundImage:(nullableUIImage *)backgroundImage forState:(UIControlState)state;//设置文本框的背景图片,及什么状态显示
- (nullable UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;//获取指定状态的背景图片
- (void)setImage:(nullableUIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;//设置图标
- (nullable UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;//获取图标
//自定义范围按钮的外观
- (void)setScopeBarButtonBackgroundImage:(nullableUIImage *)backgroundImage forState:(UIControlState)state;//设置范围按钮的背景图片,及什么状态显示
- (nullable UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state;//获取指定状态的范围按钮背景
- (void)setScopeBarButtonDividerImage:(nullableUIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightStateNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;
//设置范围按钮的分隔背景图片
- (nullable UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightStateNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;
//获取范围按钮的分割背景图片
- (void)setScopeBarButtonTitleTextAttributes:(nullableNSDictionary<NSString *,id> *)attributes forState:(UIControlState)stateNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//设置范围按钮标题文本的属性及指定什么状态显示
- (nullable NSDictionary<NSString *,id> *)scopeBarButtonTitleTextAttributesForState:(UIControlState)stateNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//获取范围按钮指定状态标题文本属性
@property(nonatomic)UIOffset searchFieldBackgroundPositionAdjustmentNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//调整文本框相对于搜索栏的位置
@property(nonatomic)UIOffset searchTextPositionAdjustmentNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//调整搜索的文本相对于文本框的位置
- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)iconNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//设置文本框指定类型图标的相对于文本框的位置
- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)iconNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;//获取文本框指定类型图标的相对于文本框的位置
@end
//UISearchBar协议
@protocol UISearchBarDelegate <UIBarPositioningDelegate>
@optional
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; //
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; //
文本框获取焦点后调用的
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; //
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // 文本框失去焦点调用的
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // 输入文本实时更新调用的方法
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0);// called before text changes
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0);
//最后三个事件根据实际显示的图标进行实现
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // 键盘上的搜索按钮点击事件,他比较特殊
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // 书本图标的按钮单击事件
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar; // 取消按钮的单击事件
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2);//搜索结果列表图标的单击事件
@end