3

In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...

enter image description here

Emon
  • 958
  • 3
  • 10
  • 28

5 Answers5

9

you can subclass the UISearchBar and override the layoutSubviews method :

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

   [super layoutSubviews];
}

Also you can :

//to clear searchbar backgraound
- (void) clearSearchBarBg
{
    for (UIView *subview in theSearchBar.subviews) 
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
        {
            [subview removeFromSuperview];
            break;
        }
    }
}

//display showSearchButtonInitially in a keyboard 
- (void)showSearchButtonInitially
{
    UIView * subview;
    NSArray * subviews = [theSearchBar subviews];

    for(subview in subviews)
    {
        if( [subview isKindOfClass:[UITextField class]] )
        {
            NSLog(@"setEnablesReturnKeyAutomatically");
            [((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
            ((UITextField*)subview).delegate=self;
            [((UITextField*)subview) setEnabled:TRUE];
            ((UITextField*)subview).borderStyle = UITextBorderStyleNone;
            break;
        }
    }
}
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • Actually i have got you what do you want to say but if you explain it then it will too much helpful for me...thanks for quick reply. – Emon Dec 28 '11 at 11:47
  • I will accept your answer if you explain it actually i am beginner at iPhone application Development...so please explain, i have to solve it quickly. – Emon Dec 28 '11 at 12:05
  • the above code is accessing the subviews of search bar such as text filed, background of text field .... simple – Maulik Dec 28 '11 at 12:08
2

Look for Apple DOC for UISearchBar

enter image description here

You have bunch of methods there to get whatever you want

You can get UITextView Inside the search bar by

UITextField *textField = [searchBar.subviews objectAtIndex:2];

if ([textField isKindOfClass:[UITextField class]]) {
    //Do your customization
}

Again look for AppleDoc for UITextField. You have bunch of methods for that also.

MadNik
  • 7,713
  • 2
  • 37
  • 39
0
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];

here searchBarDesign is my searchBar name.

IronManGill
  • 7,222
  • 2
  • 31
  • 52
0

I think it's better just set all properties of UISearchBar when it is loaded.

@interface MySearchBar : UISearchBar

@end


@implementation MySearchBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self myInitialize];
    }
    return self;
}

-(void)awakeFromNib

{
    [super awakeFromNib];
    [self myInitialize];
}

-(void)myInitialize
{
    self.backgroundImage = [UIImage imageNamed:@"image.png"];

    for (UIView* subview in self.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
            //customize text field
            UITextField* textfield = (UITextField*) subview;
        }
    }
}

@end
Hepeng Zhang
  • 141
  • 1
  • 3
0

Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55