UISearchBar 改變編輯去背景顏色

1. 使用此代碼改變搜索欄“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 == 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種UISearchBarIconUISearchBarIconBookmarkUISearchBarIconClearUISearchBarIconResultsListUISearchBarIconSearch我希望這可以幫助您... 

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]];
 }
}


發佈了32 篇原創文章 · 獲贊 5 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章