Uisearchbar change edit to Background color

Source: Internet
Author: User

1.Use this code to change the search bar UITextFieldwill 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];}
theCorrugated PipeChanges in code UISearchBarIcon
 UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];searchIcon.frame = CGRectMake(10, 10, 24, 24);[searchBar addSubview:searchIcon];[searchIcon release];
also for changing the Searcbar icon Search bar which of your available iOS 5 +
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
Here you can set up 4 kinds of UISearchBarIconthat UISearchBarIconBookmark UISearchBarIconClear UISearchBarIconResultsList UISearchBarIconSearchI hope this can help you ...

2.According to the Uisearchbar you this function applies to iOS 5.0 +.
- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state
Use cases:
[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];
Sadly, in iOS 4 you need to revert to the other answers that are less visible.

3.just the custom text field itself. I'm just doing this and it works fine (iOS 7).
UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];txfSearchField.backgroundColor = [UIColor redColor];
this way, you don't need to create an image, its size, and so on .

4.for changing colors only:
searchBar.tintColor = [UIColor redColor];
to apply a background image:
[self.searchBar setSearchFieldBackgroundImage:       [UIImage imageNamed:@"Searchbox.png"]          forState:UIControlStateNormal];


5.for private APIs:
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) { if ([subview isKindOfClass:[UITextField class]]) {  UITextField *textField = (UITextField*)subview;  redColor]]; }}


Uisearchbar change edit to Background color

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.