change search magnifying glass to UIActivityIndicatorView

╄→гoц情女王★ 提交于 2020-01-04 05:30:12

问题


I have an app that searches through 13,000 cells for text. I know it is a lot of cells. On older iphones the search takes multiple seconds so I wanted to provide an indicator view that showed the user that the app was still working. I came up with the idea of changing the UISearchBar magnifying glass to a UIActivityIndicatorView. The code works in the simulator but the spinner doesn't show up when I test on an old ipod touch. It actually does show up but only after the search is complete. Any idea as to why? Here is the code.

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
UIActivityIndicatorView *spin = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGRect spinnerFrame = CGRectMake(12.0, 12.0, 20.0, 20.0);
spin.frame = spinnerFrame;
spin.clipsToBounds = YES;
spin.backgroundColor = [UIColor whiteColor];
[searchBar addSubview:spin];
[spin startAnimating];
[self performSelectorOnMainThread:@selector(filterContentForSearchText:) withObject:searchBar.text waitUntilDone:YES];
[spin stopAnimating];
[spin removeFromSuperview];
//[self filterContentForSearchText:searchBar.text];
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
[self.searchDisplayController.searchResultsTableView reloadData];

}


回答1:


Replace your searchbar with this:

@interface _SearchBarWithSpinner : UISearchBar
{
    UIActivityIndicatorView *_spinnerView;
    UIView                  *_searchIconView;
    UITextField             *_internalTextField;
}

- (void)showSpinner;
- (void)hideSpinner;

@end

@implementation _SearchBarWithSpinner

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldDidBeginEditingNotification:)
                                                     name:UITextFieldTextDidBeginEditingNotification
                                                   object:nil];
    }

    return self;
}

- (void)showSpinner
{
    if(_internalTextField)
    {
        if(_spinnerView == nil)
            _spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        [_internalTextField setLeftView:_spinnerView];
        [_spinnerView startAnimating];
    }
}

- (void)hideSpinner
{
    [_spinnerView stopAnimating];
    [_internalTextField setLeftView:_searchIconView];
}

#pragma mark - Private

- (void)textFieldDidBeginEditingNotification:(NSNotification *)notification
{
    if(_internalTextField == nil)
    {
        UITextField *editedTextField    = notification.object;
        UIView      *superView          = editedTextField.superview;

        while(superView && superView != self)
            superView = superView.superview;

        if(superView == self)
        {
            _internalTextField  = editedTextField;
            _searchIconView     = _internalTextField.leftView;
        }
    }
}

@end


来源:https://stackoverflow.com/questions/5109272/change-search-magnifying-glass-to-uiactivityindicatorview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!