The UISearchBar search box component basic use guide in iOS

  • 2020-06-01 11:04:18
  • OfStack

UISearchBar is also one of the common controls that iOS develops. Click on it to see the properties barStyle, text, placeholder, and so on. However, these properties are not enough to meet our development needs. For example, change the color of placeholder, change the background color of UITextfield above UISearchBar, change the photo above UITextfield, etc.

To do this, write a subclass of UISearchBar called LSSearchBar

LSSearchBar. h as follows:


#import <UIKit/UIKit.h> @interface LSSearchBar : UISearchBar @end

LSSearchBar. m as follows:


#import "LSSearchBar.h" @implementation LSSearchBar - (void)layoutSubviews {     [super layoutSubviews];     // By iterating through self.subviews find searchField
    UITextField *searchField;
    NSUInteger numViews = [self.subviews count];
    for(int i = 0; i < numViews; i++) {
        if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
            searchField = [self.subviews objectAtIndex:i];
        }
    }     // If the above method is not found searchField , then try the following method     if (searchField ==  nil) {
        NSArray *arraySub = [self subviews];
        UIView *viewSelf = [arraySub objectAtIndex:0];
        NSArray *arrayView = [viewSelf subviews];
        for(int i = 0; i < arrayView.count; i++) {
            if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {
                searchField = [arrayView objectAtIndex:i];
            }
        }
    }
    if(!(searchField == nil)) {
        // Set the color
        searchField.textColor = [UIColor whiteColor];         // Set the background color
        [searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
        [searchField setBorderStyle:UITextBorderStyleNone];         // Set up the placeholder The color of the
        [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];         // Set up the searchField The photo on the
        UIImage *image = [UIImage imageNamed:@"search"];
        UIImageView *iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(0, 0, 15, 15);
        searchField.leftView = iView;
    } } @end

Modify the UISearchBar background color
ISearchBar is composed of two subView, one is UISearchBarBackGround, the other is UITextField. The way to do this is to simply remove the UISearchBarBackGround


seachBar=[[UISearchBar alloc] init]; 
seachBar.backgroundColor=[UIColor clearColor]; 
for (UIView *subview in seachBar.subviews){   
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  {   
        [subview removeFromSuperview];   
    break; 
    }  
}

UISearchBar text color change
1. To access the text field in iOS 7, you must reiterate more at the level. Change your code like this


for (UIView *subView in self.searchBar.subviews)
{
 for (UIView *secondLevelSubview in subView.subviews){
  if ([secondLevelSubview isKindOfClass:[UITextField class]])
  {
   UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
   //set font color here
   searchBarTextField.textColor = [UIColor blackColor];
   break;
  }
 }
}

Or you can set tintcolor to apply the key in search bar. Use tintColor to color the foreground using barTintColor to color the column background. In iOS system V7.0, a subclass of UIView derived from the base class behavior tintColor. See tintColor for UIView level apple files
2. You can set the color of the text

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

3. While it is true that the UIAppearance protocol is an "open API," it is not true that UITextField supports this 1 point. If you look at 1 at UITextField.h and look for the string "UI_APPEARANCE_SELECTOR", you will see that it has any instance of this string. If you look at UIButton CodeGo.net, you'll see quite a few -- these are all properties officially supported by the UIAppearance API. It is well known that UITextField is not supported by UIAppearance API, so the answer code in sandeep is not always feasible and it is not actually the best method. This is the link to the post: the correct way to do this is to - traverse the subview (or subview mainly for IOS7's subview) and manually set it. Otherwise, you will have unreliable results. But you can create 1 category of UISearchBar and add setTextColor: (* UIColor) example:

- (void)setTextColor:(UIColor*)color
{
 for (UIView *v in self.subviews)
 {
  if([Environment isVersion7OrHigher]) //checks UIDevice#systemVersion   
  {
   for(id subview in v.subviews)
   {
    if ([subview isKindOfClass:[UITextField class]])
    {
     ((UITextField *)subview).textColor = color;
    }
   }
  }
  else
  {
   if ([v isKindOfClass:[UITextField class]])
   {
    ((UITextField *)v).textColor = color;
   }
  }
 }
}

Customize the background image of UISearchBar


- (void)layoutSubviews {
    UITextField *searchField;
    NSUInteger numViews = [self.subviews count];
    for(int i = 0; i < numViews; i++) {
        if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
            searchField = [self.subviews objectAtIndex:i];
        }
    }
    if(!(searchField == nil)) {
        searchField.textColor = [UIColor whiteColor];
        [searchField.leftView setHidden:YES];
        [searchField setBackground: [UIImage imageNamed:@"SearchBarBackground.png"] ];
        [searchField setBorderStyle:UITextBorderStyleNone];
    }
     
    [super layoutSubviews];
}


Related articles: