一、简介
<<继承关系:UISearchBar-->UIView-->UIResponder-->NSObject
格式为
1-->搜索框风格(作用)
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // currently UISearchBarStyleProminent
UISearchBarStyleProminent, // used my Mail, Messages and Contacts
UISearchBarStyleMinimal // used by Calendar, Notes and Music
} NS_ENUM_AVAILABLE_IOS(7_0);
searchbar.barStyle = UIBarStyleBlack; (这是具体的例子)
@property(nonatomic) UIBarStyle barStyle;// 搜索框风格 (这是说明)
二、UISearchBar的初始化方法(属性的顺序与苹果API一致)
1-->三种初始化方法(三个初始化方法是我们常见的初始化 UIView 以及它的子类的方法,比较常见)
-(instancetype)init
-(instancetype)initWithFrame:(CGRect)frame
-(nullable instancetype)initWithCoder:(NSCoder *)aDecoder
三、UISearchBar的初始化方法
1、设置搜索框风格
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault //白色搜索框,灰色背景
UIBarStyleBlack //黑色搜索框,
UIBarStyleBlackOpaque = 1, // 禁用. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // 禁用. Use UIBarStyleBlack and set the translucent property to YES
}
searchbar.barStyle = UIBarStyleBlack;
@property(nonatomic) UIBarStyle barStyle;// 搜索框风格
2-->设置代理
searchbar.delegate = self;
@property(nullable,nonatomic,weak) id<UISearchBarDelegate> delegate;
3-->设置搜索的文本
searchBar.text = @"ask";
@property(nullable,nonatomic,copy) NSString *text; // 获取搜索输入内容
4-->顶部提示文本
searchbar.prompt = @"iOS Dev";
@property(nullable,nonatomic,copy) NSString *prompt; //默认是空
5-->设置占位符
searchbar.placeholder = @"请输入搜索关键字";
@property(nullable,nonatomic,copy) NSString *placeholder; // 默认是空
6-->是否显示书签按钮,默认隐藏
searchbar.showsBookmarkButton = YES;
@property(nonatomic) BOOL showsBookmarkButton __TVOS_PROHIBITED; // 默认是 NO
注意:书签按钮属性与搜索回车按钮属性不能同时进行设置,只能二选一,否则会出现冲突)
7-->是否显示退出按钮
searchbar.showsCancelButton = YES;
@property(nonatomic) BOOL showsCancelButton __TVOS_PROHIBITED; 是否显示退出按钮,默认隐藏(注意:通常退出按钮的显示是在代理方法中设置其显示或隐藏,而不是在初始化时就设置基显示)
8-->是否显示搜索回车按钮
searchbar.showsSearchResultsButton = YES;
@property(nonatomic) BOOL showsSearchResultsButton NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; //是否显示搜索回车按钮,默认隐藏(注意:书签按钮属性与搜索回车按钮属性不能同时进行设置,只能二选一,否则会出现冲突)
9-->搜索结果按钮为选中状态
searchbar. searchResultsButtonSelected = YES;
@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // 默认是no , 设置搜索结果按钮的选中状态
10-->设置显示取消按钮
[searchBar setShowsCancelButton:YES animated:YES];
- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
11-->输入助手配置键盘的快捷方式栏时使用
UITextInputAssistantItem* item = [searchBar inputAssistantItem];
@property (nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
12-->设置这个颜色值会影响搜索框中的光标的颜色
searchbar.tintColor = [UIColor redColor]; // 输入光标颜色
@property(nonatomic,retain) UIColor *tintColor;
13-->设置输入框边框颜色
searchbar.barTintColor = [UIColor brownColor];
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
14-->设置输入框类型
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // currently UISearchBarStyleProminent
UISearchBarStyleProminent, // 显示背景
UISearchBarStyleMinimal // 不显示背景
} NS_ENUM_AVAILABLE_IOS(7_0);
searchbar.searchBarStyle = UISearchBarStyleProminent;
@property (nonatomic) UISearchBarStyle searchBarStyle NS_AVAILABLE_IOS(7_0);
15-->设置是否半透明
searchBar. translucent = YES;
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0); // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
16-->搜索栏下部的选择栏,数组里面的内容是按钮的标题
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"BOY",@"GIRL",@"ALL",nil];
@property(nullable, nonatomic,copy) NSArray<NSString *> *scopeButtonTitles NS_AVAILABLE_IOS(3_0);
17-->搜索栏下部的选择栏按钮的个数
searchBar.selectedScopeButtonIndex = 0;//默认选中第1个啦
@property(nonatomic) NSInteger selectedScopeButtonIndex NS_AVAILABLE_IOS(3_0);
18-->控制搜索栏下部的选择栏是否显示出来
searchBar.showsScopeBar = YES;
@property(nonatomic) BOOL showsScopeBar NS_AVAILABLE_IOS(3_0); // 默认是NO的。如果YES,则显示范围栏
19--> 输入框成为第一响应显示自定义附件输入视图
UILabel *label = [[UILabel alloc]init];
label.text = @"这是一个inputAccessoryView";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.frame = CGRectMake(0, 0, 0, 30);
label.backgroundColor = [UIColor redColor];
search.searchBar.inputAccessoryView = label;
@property (nullable, nonatomic, readwrite, strong) UIView *inputAccessoryView;
20-->搜索框的背景图
UIImage *backgroundImage = [UIImage imageNamed:@"image001"];
self.backgroundImage = backgroundImage;
@property(nullable, nonatomic,strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
21-->搜索框附属分栏条的背景
UIImage *scopeBarBackgroundImage = [UIImage imageNamed:@"image002"];
self.scopeBarBackgroundImage = scopeBarBackgroundImage;
@property(nullable, nonatomic,strong) UIImage *scopeBarBackgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
22-->设置搜索框背景图片
typedef NS_ENUM(NSInteger, UIBarPosition) {
UIBarPositionAny = 0,//不指定位置
UIBarPositionBottom = 1, // 位于视图底部
UIBarPositionTop = 2, //位于视图顶部
UIBarPositionTopAttached = 3, // 位于屏幕顶部同时也在其视图的顶部
} NS_ENUM_AVAILABLE_IOS(7_0);
typedef NS_ENUM(NSInteger, UIBarMetrics) {
UIBarMetricsDefault,//设备默认的外观
UIBarMetricsCompact,//手机尺寸的外观
UIBarMetricsDefaultPrompt = 101, //横屏且设置了prompt属性
UIBarMetricsCompactPrompt,//竖屏且设置了prompt属性
UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,
UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,
};
[self setBackgroundImage:[UIImage imageNamed:@"image001"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
-(void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
23-->获取搜索框背景图片
UIImage *image=[searchBar backgroundImageForBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
-(nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
24、设置搜索框文本框的背景图片
[self setSearchFieldBackgroundImage:[UIImage imageNamed:@"image003"] forState:UIControlStateNormal];
-(void)setSearchFieldBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
25、 获取搜索框文本框的背景图片
UIImage *image=[searchBar searchFieldBackgroundImageForState:UIControlStateNormal];
-(nullable UIImage *)searchFieldBackgroundImageForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
26、设置搜索框的图标
[self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
-(void)setImage:(nullable UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
27、获取搜索框的图标
UIImage *image=[searchBar imageForSearchBarIcon: UISearchBarIconSearch state: UIControlStateNormal];
-(nullable UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
28、设置选择按钮视图的背景图片
[self setScopeBarButtonBackgroundImage:[UIImage imageNamed:@"image002"] forState:UIControlStateNormal];
-(void)setScopeBarButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
29、获取选择按钮视图的背景图片
UIImage *image=[searchBar scopeBarButtonBackgroundImageForState:UIControlStateNormal];
-(nullable UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
30、设置选择按钮视图的分割线图片
[self setScopeBarButtonDividerImage:[UIImage imageNamed:@"divider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
-(void)setScopeBarButtonDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
31、获取选择按钮视图的分割线图片
UIImage *image=[searchBar scopeBarButtonDividerImageForLeftSegmentState:UIControlStateNormal rightSegmentState: UIControlStateNormal];
-(nullable UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
32、设置选择按钮视图的标题样式
NSDictionary *attributeDic = @{NSFontAttributeName : [UIFont systemFontOfSize:20] , NSStrokeColorAttributeName : [UIColor orangeColor] , NSStrokeWidthAttributeName : @(3)};
[self setScopeBarButtonTitleTextAttributes:attributeDic forState:UIControlStateNormal];
-(void)setScopeBarButtonTitleTextAttributes:(nullable NSDictionary<NSString *, id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
32、获取选择按钮视图的标题样式
NSDictionary *dic=[searchBar scopeBarButtonTitleTextAttributesForState: UIControlStateNormal ];
- (nullable NSDictionary<NSString *, id> *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
33、 搜索框中文本框的背景偏移量
self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(5, 3);
@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
34、搜索框中文本框的文本偏移量
self.searchTextPositionAdjustment = UIOffsetMake(10, 5);
@property(nonatomic) UIOffset searchTextPositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
35、设置搜索框图标的偏移量
[self setPositionAdjustment:UIOffsetMake(15, 5) forSearchBarIcon:UISearchBarIconSearch];
-(void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
36、获取搜索框图标的偏移量
UIOffset *offset=[searchBar positionAdjustmentForSearchBarIcon:UISearchBarIconSearch];
-(UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
四、UISearchBar的UISearchBarDelegate属性
1-->将要开始编辑文本时会调用该方法,返回 NO 将不会变成第一响应者
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
return YES;
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
2-->开始输入文本会调用该方法
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(@"开始输入搜索内容");
[searchBar setShowsCancelButton:YES animated:YES]; // 动画显示取消按钮
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
3-->将要结束编辑文本时会调用该方法,返回 NO 将不会释放第一响应者
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
4-->结束编辑文本时调用该方法
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
searchBar.prompt = @"已经结束编辑文本";
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
5-->文本改变会调用该方法(包含clear文本)
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
searchBar.prompt = @"2.在改变文本过程中。。。";
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
6-->文字改变前会调用该方法,返回NO则不能加入新的编辑文字
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return YES;
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0);
7-->键盘上的搜索按钮点击的会调用该方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
searchBar.prompt = @"点击键盘上的搜索按钮";
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
8-->搜索框右侧图书按钮点击会调用该方法
-(void) searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
searchBar.prompt = @"点击图书按钮";
}
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
9-->点击取消按钮会调用该方法
-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.prompt = @" 点击取消按钮";
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
10-->搜索结果列表按钮被按下会调用该方法
-(void) searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
searchBar.prompt = @" 点击搜索结果列表按钮";
}
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
11-->搜索框的附属按钮视图中切换按钮会调用该方法
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
NSLog(@"selectedScope = %ld",selectedScope);
}
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);