创建
UISearchBar * searchBar = [[UISearchBar alloc] init];
UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 50, 200, 30)];
样式
- 风格
searchBar.barStyle = UIBarStyleBlack;
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0, // 默认样式
UIBarStyleBlack = 1, // 黑色背景样式
UIBarStyleBlackOpaque = 1, // 被弃用,使用UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // 被弃用,使用UIBarStyleBlack 并设置translucent属性为YES
} __TVOS_PROHIBITED;
- 样式
该属性可以与barStyle
属性共同使用
searchBar.searchBarStyle = UISearchBarStyleMinimal;
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // 当前UISearchBarStyleProminent样式
UISearchBarStyleProminent, // 用于我的邮件、消息和联系人
UISearchBarStyleMinimal // 用于我的日历、笔记和音乐
} NS_ENUM_AVAILABLE_IOS(7_0);
UISearchBarStyleMinimal
不提供默认背景颜色或样式,我们可以定制,自定义背景和搜索框图片优先级高于该属性
- 搜索栏上关键元素的色调
searchBar.tintColor = [UIColor redColor];
- 搜索栏整体的色调
searchBar.barTintColor = [UIColor yellowColor];
// 会改变cancel按钮的title颜色
- 半透明
searchBar.translucent = NO; // 默认为YES
- 背景图
searchBar.backgroundImage = [UIImage imageNamed:@"bg.png"];
// 使用UIBarMetrics、DefaultPrompt设置一个背景图
-(void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// 获取给定UIBarMetrics、DefaultPrompt的背景图
-(nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
- 输入框背景图
// 设置搜索框区域的背景图
-(void)setSearchFieldBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取某状态下搜索框区域的背景图
-(nullable UIImage *)searchFieldBackgroundImageForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 设置各按钮图标
// 给搜索框中的查找、清除、数钱、结果列表按钮设置图标
-(void)setImage:(nullable UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取某个按钮某个状态下的图标
-(nullable UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
其他属性
- 当前内容
searchBar.text;
- 占位符
searchBar.placeholder = @"请输入搜索内容...";
- 显示在搜索栏上方的一行文字
searchBar.prompt = @"你好";
- 是否显示书签图标
searchBar.showsBookmarkButton = YES; // 默认为NO
- 是否显示取消按钮
searchBar.showsCancelButton = YES; // 默认为NO
或者,可以使用下面的方法进行设置显示与隐藏,并且可以设置是否带有显示隐藏动画
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
- 是否显示搜索结果按钮
searchBar.showsSearchResultsButton = YES; // 默认为NO
// 当该属性被设置为YES时,showsBookmarkButton属性设置失效
还可以控制搜索结果按钮的选中状态
searchBar.searchResultsButtonSelected = YES; //默认为NO
- 键盘的输入配置(只读)
searchBar.inputAssistantItem;
- 输入键盘附加视图
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
view.backgroundColor = [UIColor redColor];
// 在键盘上附加一个视图,一般用于添加一个收回键盘的按钮
searchBar.inputAccessoryView = view;
偏移量
- 文字相对于输入框背景的偏移量
searchBar.searchTextPositionAdjustment = UIOffsetMake(10, 10);
- 输入框背景相对于整个searchBar的偏移量
searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(10, 10);
- 按钮图标相对于输入框背景的偏移量
// 设置按钮图标相对于输入框背景的偏移量
-(void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取按钮图标相对于输入框背景的偏移量
-(UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
[searchBar setPositionAdjustment:UIOffsetMake(10, 10) forSearchBarIcon:UISearchBarIconSearch];
扩展栏
- 显示扩展栏
searchBar.showsScopeBar = YES; // 默认NO
- 扩展栏标题
searchBar.scopeButtonTitles = @[@"组一",@"组二",@"组三"];
- 当前选中的扩展栏模块序号
searchBar.selectedScopeButtonIndex = 1;
- 背景图
searchBar.scopeBarBackgroundImage = [UIImage imageNamed:@"scopeBg.png"];
- 按钮背景图
// 设置扩展栏按钮背景图
-(void)setScopeBarButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取扩展栏按钮背景图
-(nullable UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 设置按钮标题富文本样式
-(void)setScopeBarButtonTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
-(nullable NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 设置按钮间分割线图片
// 需要设置:左选中右正常、左正常右选中、左正常右正常等状态时分割线的图片
-(void)setScopeBarButtonDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
-(nullable UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
代理
@property(nullable,nonatomic,weak) id delegate;
// 通过返回值设置searchBar是否是第一响应者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
// 当searchBar输入框中的文字开始编辑时调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
// 通过返回值设置searchBar是否结束编辑
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
// 当searchBar结束编辑时调用
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
// 当searchBar输入框中的文字发生变化时调用(包括清除操作)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
// 当searchBar输入框中文字在发生变化之前调用
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0);
// 当点击键盘中的搜索按钮时调用
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
// 当点击searchBar中的书签按钮时调用
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
// 当点击searchBar中的取消按钮时调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
// 当点击searchBar中的搜索结果按钮时调用
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED;
// 当点击searchBar扩展栏中的按钮时调用
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);
版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!