天天看點

如何更改iOS上的UISearchBar元件的内部背景顔色

如何更改iOS上的UISearchBar元件的内部背景顔色

1. 使用此代碼改變搜尋欄“UITextField将backgroundImageUITextField *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 == nil)) {

searchField.textColor = [UIColor whiteColor];

[searchField setBackground: [UIImage imageNamed:@"yourImage"]];//just add here gray image which you display in quetion

[searchField setBorderStyle:UITextBorderStyleNone];

該波紋管代碼的變化UISearchBarIcon UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];

searchIcon.frame = CGRectMake(10, 10, 24, 24);

[searchBar addSubview:searchIcon];

[searchIcon release];

也為改變searcBar圖示搜尋欄的你哪些可用的iOS 5 +- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state

在這裡你可以設定4種UISearchBarIcon即UISearchBarIconBookmarkUISearchBarIconClearUISearchBarIconResultsListUISearchBarIconSearch我希望這可以幫助您... 

2. 根據的UISearchBar 你這個函數适用于iOS 5.0 +。- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state

用例:[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];

可悲的是,在iOS 4的你需要恢複到較少見其他的答案。 

3. 隻是自定義文本字段本身。 我隻是這樣做的,它工作正常(的iOS 7)。UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];

txfSearchField.backgroundColor = [UIColor redColor];

這樣,您就不需要建立一個圖像,它的大小,等.. 

4. 對于隻改變顔色:searchBar.tintColor = [UIColor redColor];

為應用背景圖像:[self.searchBar setSearchFieldBackgroundImage:

[UIImage imageNamed:@"Searchbox.png"]

forState:UIControlStateNormal];

5. 私有API的:for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {

if ([subview isKindOfClass:[UITextField class]]) {

UITextField *textField = (UITextField*)subview;

[textField setBackgroundColor:[UIColor redColor]];

本文轉自 卓行天下  51CTO部落格,原文連結:http://blog.51cto.com/9951038/1746325,如需轉載請自行聯系原作者

繼續閱讀