Customizing search bar in iPhone application Development

 
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;
       
}
   
}
}
 
All the best. 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>