IOS UI学习 UISearchBar

UISearchBar继承自UIView、UIResponder、NSObject

UISearchBar属性设置

code:

 1 -(void)createSearchBar
 2 {
 3     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
 4     //设置控件样式
 5     _searchBar.barStyle = UISearchBarStyleProminent;
 6     
 7     //自动对文本对象进行大小写设置
 8     //包含四中类型
 9     /*
10     typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
11         UITextAutocapitalizationTypeNone,
12         UITextAutocapitalizationTypeWords,
13         UITextAutocapitalizationTypeSentences,
14         UITextAutocapitalizationTypeAllCharacters,
15     };
16     */
17     _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
18     
19     //自动对输入文本进行纠错
20     //一共三种
21     /*
22     typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
23         UITextAutocorrectionTypeDefault,
24         UITextAutocorrectionTypeNo,
25         UITextAutocorrectionTypeYes,
26     }
27     */
28     _searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
29     
30     //控件背景图片
31     _searchBar.backgroundImage = [UIImage imageNamed:@""];
32     
33     //控件右端显示一个 cancel 按钮 默认显示
34     _searchBar.showsCancelButton = YES;
35     
36     //在控件的右端显示一个 搜索结果 按钮
37     _searchBar.showsSearchResultsButton = YES;
38     
39     //搜索结果 按钮是否被选中
40     _searchBar.searchResultsButtonSelected = NO;
41 
42     //控件右端显示一个 书的 按钮 输入文字时消失
43     _searchBar.showsBookmarkButton = YES;
44     
45     //控件上面显示的文字
46     //_searchBar.text = @"2";
47     
48     //设置控件是否具有透视效果
49     _searchBar.translucent = NO;
50     
51     //设置占位文字 (提示文字) 输入文字时消失
52     _searchBar.placeholder = @"请输入文字";
53     
54     //设置输入时的键盘样式
55     _searchBar.keyboardType = UIKeyboardTypeNumberPad;
56     
57     //设置显示在控件顶部的一行提示文字
58     _searchBar.prompt = @"I am searchBar";
59     
60     //设置Bar的颜色
61     _searchBar.tintColor = [UIColor greenColor];
62     //设置代理 <UISearchBarDelegate>
63     _searchBar.delegate = self;
64     
65     //搜索栏下面的选择栏  数组里面的内容是按钮的标题
66     //_searchBar.scopeButtonTitles;
67     
68     //搜索栏下面的选择栏按钮的个数
69     _searchBar.selectedScopeButtonIndex = 3;
70     
71     //是否在搜索栏下面显示  选择栏  需要设置为 yes 才能使用 scopebar
72     _searchBar.showsScopeBar = YES;
73     
74     self.navigationItem.titleView = _searchBar;
75 }

UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击按钮 或者手势之类 后,代理方法会完成搜索动作。

 

未完待续

posted @ 2015-09-14 10:47  cccccy  阅读(136)  评论(0编辑  收藏  举报