UISearchBar常用属性

前言

记录下常用的属性,方便查阅

正文

  • 设置取消按钮文本
if (IOS9) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[BESearchUserByNameVC class]]] setTitle:BluedLocalizedStringWithKey(@"gjp_cancel")];
} else {
        [[UIBarButtonItem appearanceWhenContainedIn: [BESearchUserByNameVC class], nil] setTitle:BluedLocalizedStringWithKey(@"gjp_cancel")];
}
  • 设置取消按钮颜色
searchBar.tintColor = [UIColor gjw_colorWithHex:0x616aff];
  • 设置搜索框的背景,不是整个searchBar的背景
 [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"common_searchBar_bg"] forState:UIControlStateNormal];
  • 替换系统的搜索图标
[searchBar setImage:[UIImage imageNamed:@"common_searchBar_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
  • 设置整个searchBar的背景
searchBar.backgroundImage = [UIImage gjw_imageWithColor:[UIColor gjw_colorWithHex:0xf4f4f4] size:self.searchController.searchBar.bounds.size];
  • 获取搜索框的placeHoder,修改字体,颜色等
UITextField *searchField = [searchBar valueForKey:@"_searchField"];
[searchField setValue:[UIColor gjw_colorWithHex:0x848bff] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setValue:TEXT_FONT(14) forKeyPath:@"_placeholderLabel.font"];
  • 显式设置尺寸。在iOS8,iPhone4上使用UISearchController的默认searchBar作为UITableViewtableHeaderView时,无法显示,估计是个系统bug
searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);

效果图

搜索状态

上一篇: 去除编译警告
下一篇: git笔记

你可能感兴趣的:(UISearchBar常用属性)