Lothario
Lothario

Reputation: 300

How to insert a UIActivityIndicator into search bar textfield?

I want to add a UIActivityIndicator in the right side of textfield of the search bar. So that search bar will keep on showing loading status while it fetches data from DB.

Any idea about this?

Upvotes: 3

Views: 6112

Answers (3)

Midhun MP
Midhun MP

Reputation: 107211

Add the activity indicator as the subView of searchbar like :

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
   spinner.center = CGPointMake(160, 240);
   spinner.hidesWhenStopped = YES;
   spinner.tag = 7;
   [self.searchBar addSubview:spinner];
}

And when you want to remove the activity indicator you can use the tag.

Upvotes: 2

iDev
iDev

Reputation: 23278

Adding my comment as answer.

You can try to use [searchbar addSubview:activityIndicator]; There are no in built ways to do this. Only thing you can change is the search icon using the method setImage:forSearchBarIcon:state:. This can be used to add a different image in searchbar. Apple doc on Searchbar. A work around would be to loop through the subviews of the search bar and add the activity indicator to the subview at required place.

Upvotes: 0

Omar Freewan
Omar Freewan

Reputation: 2678

This is away to change the search magnifier with the spinner

UITextField *searchField = nil;  
for (UIView *subview in searchBar.subviews) {    
    if ([subview isKindOfClass:[UITextField class]]) {      
        searchField = (UITextField *)subview;      
        break;    
    }  
}    
if (searchField) {      
      UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

       searchField.leftView = spinner;    

} 

or if you like you can add the spinner over the search bar views

[self.searchBar addSubview:spinner];

Upvotes: 8

Related Questions