搜索框:UISearch
1、自定义UISearch,继承UITextField,UISearBar不好用。
相关属性:contentVerticalAlignment--文字居中设置
清除按钮--clearButtonMode
在左边添加一个view--leftView--显示图片放大安
placeholder--- 搜索提醒文字,(使用placeholder样式单一,无法改变样式,这里用另外一中方法)
具体事例代码:
1 // 背景
2 UITextField * searchBar = [[UITextField alloc]init];
3 searchBar.background = [UIImage resizeImageWithName:@"searchbar_textfield_background"];
4 // 尺寸
5 searchBar.frame = CGRectMake(0, 0, 300, 30);
6 // 左边放大静
7 UIImageView * iconView = [[UIImageView alloc]initWithImage:[UIImage imageWithNamed:@"searchbar_textfield_search_icon" ]];
8 // 尺寸
9 iconView.frame = CGRectMake(0, 0, 30, 30);
10 iconView.contentMode = UIViewContentModeCenter;
11 searchBar.leftView = iconView;
12 searchBar.leftViewMode = UITextFieldViewModeAlways;
13 // 字体
14 searchBar.font = [UIFont systemFontOfSize:13];
15 // 添加删除按钮
16 searchBar.clearButtonMode = UITextFieldViewModeAlways;
17 // 设置提醒文字
18 NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
19
20 attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
21 searchBar.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"搜索" attributes:attrs];
22 // 设置中间的标题内容
23 self.navigationItem.titleView = searchBar;
2、当在搜索栏,弹出的键盘右下角显示搜索按钮:
// 设置键盘右下角按钮的样式 self.returnKeyType = UIReturnKeySearch;
注意:①如果想要显示汉字“搜索”,首先要将模拟器的语言换成中文,在切换键盘输入法为简体拼音
②当搜索栏没有任何输入时,键盘上的搜索按钮不应能点击,需要设置一个属性
self.enablesReturnKeyAutomatically = YES;