如何在iOS上更改UISearchBar组件的内部背景颜色 [英] How to change inside background color of UISearchBar component on iOS
问题描述
我知道如何在搜索字段周围删除/更改 UISearchBar
背景颜色:
I know how to remove/change UISearchBar
background color around search field:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
self.searchBar.backgroundColor = [UIColor grayColor];
但不知道如何在其中执行此操作:
But don't know how to do this inside it like that:
这需要是兼容iOS 4.3 +。
This needs to be compatible with iOS 4.3+.
推荐答案
使用此代码更改searchBar的 UITextField
backgroundImage:
Use this code to change the searchBar's UITextField
backgroundImage:
UITextField *searchField;
NSUInteger numViews = [searchBar.subviews count];
for (int i = 0; i < numViews; i++) {
if ([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if (searchField) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage"]]; //set your gray background image here
[searchField setBorderStyle:UITextBorderStyleNone];
}
使用以下代码更改 UISearchBarIcon
:
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[searchBar addSubview:searchIcon];
[searchIcon release];
此外,要更改searchBar图标,您可以在<$ c $上使用以下内置方法c> UISearchBar (可从 iOS 5 + 获得):
Also, to change the searchBar icon you can use the following built-in method on UISearchBar
(which is available from iOS 5+):
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在这里你可以设置4种类型的 UISearchBarIcon
即:
Here you can set 4 types of UISearchBarIcon
i.e.:
-
UISearchBarIconBookmark
-
UISearchBarIconClear
-
UISearchBarIconResultsList
-
UISearchBarIconSearch
UISearchBarIconBookmark
UISearchBarIconClear
UISearchBarIconResultsList
UISearchBarIconSearch
我希望这可以帮助你...
I hope this help you...
这篇关于如何在iOS上更改UISearchBar组件的内部背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!