iOS-UIKit框架学习—UISearchBar

UISearchBar类实现一个基于文本的搜索的文本字段控制。该控件提供了一个搜索按钮,输入文字的文本字段书签“按钮,一个取消按钮。UISearchBar对象实际上并不执行任何搜索。您可以使用委托,符合UISearchBarDelegate协议的对象,实施行动时,输入文本和按钮被点击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
@protocol UISearchBarDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UISearchBar : UIView <UIBarPositioning, UITextInputTraits>
// 初始化
- (instancetype)init __TVOS_PROHIBITED;
- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER __TVOS_PROHIBITED;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER __TVOS_PROHIBITED;
// 搜索框的风格 默认蓝色
@property(nonatomic) UIBarStyle barStyle __TVOS_PROHIBITED;
// 代理
@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;
// 是否展示标签按钮 默认为NO
@property(nonatomic) BOOL showsBookmarkButton __TVOS_PROHIBITED;
// 是否显示取消按钮 默认为NO
@property(nonatomic) BOOL showsCancelButton __TVOS_PROHIBITED;
// 是否显示搜索结果的按钮 默认为NO
@property(nonatomic) BOOL showsSearchResultsButton NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED;
// 是否选择搜索结果按钮
@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED;
// 用动画设置取消按钮的显示
- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 用于配置键盘的输入助手
@property (nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
// 搜索栏中关键要素的背景底色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
// 搜索栏的背景底色
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// 搜索栏的风格
@property (nonatomic) UISearchBarStyle searchBarStyle NS_AVAILABLE_IOS(7_0);
// 搜索是否半透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0);
// 一个指示范围按钮标题的字符串的数组
@property(nullable, nonatomic,copy) NSArray<NSString *> *scopeButtonTitles NS_AVAILABLE_IOS(3_0);
// 选中指示按钮的下标
@property(nonatomic) NSInteger selectedScopeButtonIndex NS_AVAILABLE_IOS(3_0);
// 是否显示范围栏 默认值是NO
@property(nonatomic) BOOL showsScopeBar NS_AVAILABLE_IOS(3_0);
// 自定义输入框附件视图
@property (nullable, nonatomic, readwrite, strong) UIView *inputAccessoryView;
// 设置搜索栏的背景图片
@property(nullable, nonatomic,strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 设置范围栏的背景图片
@property(nullable, nonatomic,strong) UIImage *scopeBarBackgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 设置提示栏背景图片
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// 返回提示栏的背景图片
- (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;
// 设置指定范围下按钮的背景图片
- (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)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;
// 为给定状态的“搜索栏”按钮的标题字符串设置文本属性
- (void)setScopeBarButtonTitleTextAttributes:(nullable NSDictionary<NSString *, id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 返回给定状态的“搜索栏”按钮的文本属性
- (nullable NSDictionary<NSString *, id> *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 搜索栏中搜索文本字段背景的偏移量
@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 搜索文本字段背景中文本的偏移量
@property(nonatomic) UIOffset searchTextPositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 返回给定图标的位置调整
- (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;
@end
@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 *)text NS_AVAILABLE_IOS(3_0);
// 当按下键盘的搜索按钮时调用
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
// 当按下书签按钮时调用
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
// 当按下取消按钮时调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;
// 当按下搜索结果按钮时调用
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED;
// 当范围选择按钮改变是调用
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 搜索框后的图标样式
typedef NS_ENUM(NSInteger, UISearchBarIcon) {
UISearchBarIconSearch, // 放大镜
UISearchBarIconClear __TVOS_PROHIBITED, // 有一个x的圆
UISearchBarIconBookmark __TVOS_PROHIBITED, // 打开书的图标
UISearchBarIconResultsList __TVOS_PROHIBITED, // 菱形图标列表
};
// 搜索框的风格
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // currently UISearchBarStyleProminent
UISearchBarStyleProminent, // 用于邮件、消息和联系人
UISearchBarStyleMinimal // 用于日历、笔记和音乐
} NS_ENUM_AVAILABLE_IOS(7_0);