我知道如何修改和移除UISearchBar的背景顏色
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
self.searchBar.backgroundColor = [UIColor grayColor];
但是不知道怎麼修改內部的背景顏色
在IOS4.3及以上版本用的。
可以改變內部背景圖片的代碼:
UITextField *searchField;
NSUInteger numViews = [searchBar.subviews count];
for(int i = 0; i < numViews; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage"]];//在這添加灰色的圖片
[searchField setBorderStyle:UITextBorderStyleNone];
}
修改UISearchBarIcon
的代碼:
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[searchBar addSubview:searchIcon];
[searchIcon release];
在IOS5以上的版本修改searcBar圖標
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state