6

I want to change the looks of UISearchBar: So,

If there's a way by which I can make my UITextField(in a custom background for search) to function like UISearchBar ? or subclassing and overriding the - (void)layoutSubviews is the only way ?

Kindly tell How to subclass it !!!

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
nr5
  • 4,228
  • 8
  • 42
  • 82

6 Answers6

6

you can change the UISearchBar background using this bellow code..

for (UIView *subview in searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SkyBackground.jpeg"]];
            [searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }

From this code you can set image or anything else for background of UISearchBar.

Also see my this answer with some other functionality to change the searchbar component layout..How to change inside background color of UISearchBar component on iOS

UPDATE:

for change the Cancel button appearance use this bellow code...

   UIButton *cancelButton = nil;

    for(UIView *subView in searchBar.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
            //do something here with this cancel Button
        }
    }
Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • it changes the background but textfield size and and the magnifying glass and cancel button are still there. That's why I wanted a custom text field to act like UIseachBar – nr5 Jan 03 '13 at 07:19
  • just set whole background clear and also not able the cancel button to appear ..means enabled = NO – Paras Joshi Jan 03 '13 at 07:20
  • just like just [searchBar setShowsCancelButton:NO]; use this line to hide the cancel button and just do something with this code you will get output which you want ,,, also see my answer which i post the link above .. you can get something from that.. hope its helped you.. :) – Paras Joshi Jan 03 '13 at 07:22
  • 2
    Ok it worked first I removed the background of UIsearchbar then placed my own. Then used the above link of yours to change the background of UItext Field. Now the problem is I need cancel button, how can i access it to change its frame and type – nr5 Jan 03 '13 at 07:37
  • which, default cancel you want to change?? – Paras Joshi Jan 03 '13 at 07:40
  • either cancel button (yes default one) or the frame of UITextField(I tried, but height and width of textfield in serach bar is 0,0). – nr5 Jan 03 '13 at 07:49
  • now i update code for change the view or something else for cancel button of searchbar just change the code with your requirement and same as do for textfield.. :) hope this helped you.. Thank you.. – Paras Joshi Jan 03 '13 at 07:51
6

Its also possible using UITextFieldDelegates

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    
NSString *searchString;
if (string.length > 0) {        
    searchString = [NSString stringWithFormat:@"%@%@",textField.text, string];
} else {
   searchString = [textField.text substringToIndex:[textField.text length] - 1];
}
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];
resultArray = [[resultTempArray filteredArrayUsingPredicate:filter] mutableCopy];

if (!searchString || searchString.length == 0) {

    resultArray = [resultTempArray mutableCopy];
} else {
    if (resultArray.count == 0) {
NSLog(@"No data From Search");
    }
}    
[tableView reloadData];    
return YES;
}
ashokdy
  • 1,001
  • 12
  • 21
Sreekanth
  • 71
  • 1
  • 4
  • thnQ dude actually i got the same idea but not yet implemented but after seeing yours only i got confirmed that my idea is correct and also there is a small change near CONTAINS it should be SELF CONTAINS otherwise it will crash..... – ashokdy Oct 06 '14 at 13:22
2

Filtering using UItextfield delegate methods

#pragma mark - UITEXTFIELD DELEGATE METHODS -

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    arrFilterSearch = nil;
    arrFilterSearch = [NSMutableArray arrayWithArray:arrSearch];
    UITableView *tblView = (UITableView *)[self.view viewWithTag:tblView_Tag];
    [tblView reloadData];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *searchKey = [textField.text stringByAppendingString:string];
    arrFilterSearch = nil;
    searchKey = [searchKey stringByReplacingCharactersInRange:range withString:@""];
    if (searchKey.length) {
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@", searchKey];
            arrFilterSearch = [NSMutableArray arrayWithArray:[arrSearch filteredArrayUsingPredicate:pred]];
    }
    else
        arrFilterSearch = [NSMutableArray arrayWithArray:arrSearch];

    UITableView *tblView = (UITableView *)[self.view viewWithTag:tblView_Tag];
    [tblView reloadData];
    return YES;
}
Darshit Shah
  • 2,366
  • 26
  • 33
1

Try this

    self.TextFieldSearch = (UITextField *)[self.Search.subviews lastObject];
    [self.TextFieldSearch removeFromSuperview];
    [self.view addSubview:self.TextFieldSearch];
    self.TextFieldSearch.delegate = self;
    self.TextFieldSearch.font = [UIFont systemFontOfSize:10];
    self.TextFieldSearch.keyboardAppearance =  UIKeyboardAppearanceAlert;
    [self.Search removeFromSuperview];
Murali
  • 1,869
  • 1
  • 14
  • 22
0

Changing the UIsearchBar background is one way to get a customized UISearchBar. But if you want limited functionality of searching a table you can have normal text Field and hook up a search button to filter results of tableview in another array and update tabledata with that array.

UIsearchBar's - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText is almost equivalent to UItextField's valieChanged IBAction

nr5
  • 4,228
  • 8
  • 42
  • 82
0

Ok, I got the same issue, and I subclassed UISearchBar! Here you go

- (void) layoutSubviews {

[super layoutSubviews];
UITextField *searchField = nil;
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
        }

        if ([subview isKindOfClass:[UITextField class]]) {
            searchField = (UITextField *)subview;
            searchField.leftViewMode = UITextFieldViewModeNever;
            [searchField setValue:[UIFont systemFontOfSize:8] forKeyPath:@"_placeholderLabel.font"];
             [searchField setBackground: [UIImage imageNamed:@"images.png"] ];
              searchField.clearButtonMode = UITextFieldViewModeNever;
        }

}
self.showsCancelButton = NO;
}

When entering text

Cecilia Humlelu
  • 360
  • 2
  • 5