UISearchBar常用屬性

weixin_33866037發表於2017-06-08

前言

記錄下常用的屬性,方便查閱

正文

  • 設定取消按鈕文字
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);

效果圖

3302097-c54340f897500801.png
搜尋狀態

上一篇:去除編譯警告
下一篇:git筆記

相關文章