用UISearchBar中的Activityindicator替换BookmarkButton

时间:2009-07-30 22:58:04

标签: iphone uisearchbar

我使用UISearchBar输入地址以建立网络连接。在建立连接时,我想显示活动指示器,而不是搜索栏右侧的微小BookmarkButton。据我所知,没有公开声明的属性可以让我访问搜索栏的正确子视图。我已经看到这已经完成了,有什么想法吗?

6 个答案:

答案 0 :(得分:10)

在搜索或连接正在进行时,如何用活动指示器替换左侧的搜索图标?

SearchBarWithActivity.h:

#import <UIKit/UIKit.h>

@interface SearchBarWithActivity : UISearchBar

- (void)startActivity;  // increments startCount and shows activity indicator
- (void)finishActivity; // decrements startCount and hides activity indicator if 0

@end

SearchBarWithActivity.m:

#import "SearchBarWithActivity.h"

@interface SearchBarWithActivity()

@property(nonatomic) UIActivityIndicatorView *activityIndicatorView;
@property(nonatomic) int startCount;

@end


@implementation SearchBarWithActivity

- (void)layoutSubviews {
    UITextField *searchField = nil;

    for(UIView* view in self.subviews){
        if([view isKindOfClass:[UITextField class]]){
            searchField= (UITextField *)view;
            break;
        }
    }

    if(searchField) {
        if (!self.activityIndicatorView) {
            UIActivityIndicatorView *taiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            taiv.center = CGPointMake(searchField.leftView.bounds.origin.x + searchField.leftView.bounds.size.width/2,
                                      searchField.leftView.bounds.origin.y + searchField.leftView.bounds.size.height/2);
            taiv.hidesWhenStopped = YES;
            taiv.backgroundColor = [UIColor whiteColor];
            self.activityIndicatorView = taiv;
            [taiv release];
            _startCount = 0;

            [searchField.leftView addSubview:self.activityIndicatorView];
        }
    }

    [super layoutSubviews];
}

- (void)startActivity  {
    self.startCount = self.startCount + 1;
}

- (void)finishActivity {
    self.startCount = self.startCount - 1;
}

- (void)setStartCount:(int)startCount {
    _startCount = startCount;
    if (_startCount > 0)
        [self.activityIndicatorView startAnimating];
    else {
        [self.activityIndicatorView stopAnimating];
    }
}

@end

答案 1 :(得分:2)

我从@JohnLemberger更新answer以使用iOS 7(注意:我只在iOS 7上测试过这个),以及我的更改摘要:

注意:这不是一个非常强大的代码,因为Apple可以在任何版本中更改UISearchBar的视图层次结构(就像它们在iOS 6和7之间所做的那样)。 < / p>

SearchBarWithActivity.h(没有改变):

@interface SearchBarWithActivity : UISearchBar

- (void)startActivity;  // increments startCount and shows activity indicator
- (void)finishActivity; // decrements startCount and hides activity indicator if 0

@end

@interface XXTreatmentHeaderViewController : XXViewController

@property (nonatomic, strong, readonly) SearchBarWithActivity *searchBar;

@end

SearchBarWithActivity.m:

1)当活动指示符出现时显示/隐藏“放大镜”图标

2)在视图层次结构中添加深度搜索UITextField

@interface SearchBarWithActivity()

@property(nonatomic) UIActivityIndicatorView *activityIndicatorView;
@property(nonatomic) int startCount;

@end

@implementation SearchBarWithActivity

- (void)layoutSubviews {
    UITextField *searchField = nil;

    for(UIView* view in self.subviews){

        // on iOS 6, the UITextField is one-level deep
        if ([view isKindOfClass:[UITextField class]]){
            searchField= (UITextField *)view;
            break;
        }

        // on iOS 7, the UITextField is two-levels deep
        for (UIView *secondLevelSubview in view.subviews) {
            if([secondLevelSubview isKindOfClass:[UITextField class]]){
                searchField= (UITextField *)secondLevelSubview;
                break;
            }
        }
    }

    if(searchField) {
        if (!self.activityIndicatorView) {
            UIActivityIndicatorView *taiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            taiv.center = CGPointMake(searchField.leftView.bounds.origin.x + searchField.leftView.bounds.size.width/2,
                                      searchField.leftView.bounds.origin.y + searchField.leftView.bounds.size.height/2);
            taiv.hidesWhenStopped = YES;
            self.activityIndicatorView = taiv;
            _startCount = 0;

            [searchField.leftView addSubview:self.activityIndicatorView];
        }
    }

    [super layoutSubviews];
}

- (void)startActivity  {
    self.startCount = self.startCount + 1;
}

- (void)finishActivity {
    self.startCount = self.startCount - 1;
}

- (void)setStartCount:(int)startCount {
    _startCount = startCount;
    if (_startCount > 0) {
        [self.activityIndicatorView startAnimating];
        // Remove the "magnifying glass icon"
        [self setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    } else {
        [self.activityIndicatorView stopAnimating];
        // Restore the "magnifying glass icon"
        [self setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    }
}

@end

答案 2 :(得分:2)

我已经为UISearchBar实施了一个显示UIActivityIndicatorView的类别,具体取决于AFNetworking的请求操作或会话任务https://gist.github.com/nguyenhuy/a11d15c11200477b05a6的状态。

答案 3 :(得分:1)

仅供记录:

for(UIView* view in self.subviews){
    if([view isKindOfClass:[UITextField class]]){
       searchField=view;
       break;
    }
}

if(searchField !=)) {
   searchField.leftView = myCustomView;
}

您可以继承UISearchBar并在layoutSubview方法中调用此代码。在layoutSubview中调用此代码可确保调整动画大小正常工作。

答案 4 :(得分:0)

我通过使用 displaysSearchBarInNavigationBar 标志添加对UISearchBar嵌入UINavigationBar的案例的支持来更新jonsibley的answer

SearchBarWithActivity.h(添加了一个新属性):

@interface SearchBarWithActivity : UISearchBar

- (void)startActivity;  // increments startCount and shows activity indicator
- (void)finishActivity; // decrements startCount and hides activity indicator if 0

@property (nonatomic,assign) UINavigationItem *navigationItem;

@end

SearchBarWithActivity.m(如果不是nil,则从navigationItem获取searchField):

#import "SearchBarWithActivity.h"

@interface SearchBarWithActivity()

@property(nonatomic) UIActivityIndicatorView *activityIndicatorView;
@property(nonatomic) int startCount;

@end

@implementation SearchBarWithActivity

@synthesize navigationItem;

- (void)layoutSubviews {
    UITextField *searchField = nil;

    if(self.navigationItem) {
        searchField = (UITextField *)[self.navigationItem titleView];
    } else {
        for(UIView* view in self.subviews){

            // on iOS 6, the UITextField is one-level deep
            if ([view isKindOfClass:[UITextField class]]){
                searchField= (UITextField *)view;
                break;
            }

            // on iOS 7, the UITextField is two-levels deep
            for (UIView *secondLevelSubview in view.subviews) {
                if([secondLevelSubview isKindOfClass:[UITextField class]]){
                    searchField= (UITextField *)secondLevelSubview;
                    break;
                }

            }
        }
    }

    if(searchField) {
        if (!self.activityIndicatorView) {
            UIActivityIndicatorView *taiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            taiv.center = CGPointMake(searchField.leftView.bounds.origin.x + searchField.leftView.bounds.size.width/2,
                                      searchField.leftView.bounds.origin.y + searchField.leftView.bounds.size.height/2);
            taiv.hidesWhenStopped = YES;
            self.activityIndicatorView = taiv;
            _startCount = 0;

            [searchField.leftView addSubview:self.activityIndicatorView];
        }
    }

    [super layoutSubviews];
}

- (void)startActivity  {
    self.startCount = self.startCount + 1;
}

- (void)finishActivity {
    self.startCount = self.startCount - 1;
}

- (void)setStartCount:(int)startCount {
    _startCount = startCount;
    if (_startCount > 0) {
        [self.activityIndicatorView startAnimating];
        // Remove the "magnifying glass icon"
        [self setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    } else {
        [self.activityIndicatorView stopAnimating];
        // Restore the "magnifying glass icon"
        [self setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    }
}

@end

在ViewController中:

#import "SearchBarWithActivity.h"

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Embed the search bar into NavigationBar and setup the navigation item in order to show the spinner
    [self.searchDisplayController setDisplaysSearchBarInNavigationBar:YES];
    [(SearchBarWithActivity *)self.searchDisplayController.searchBar setNavigationItem:self.navigationItem];
}

我希望这能节省一些时间。

答案 5 :(得分:0)

由于看起来UITextField的深度不断变化,我想我会添加一个递归解决方案。

-(NSArray * ) findAllSubviewsForView:(UIView * ) view{
    NSMutableArray * views = [[NSMutableArray alloc] init];

    for(UIView * subview in view.subviews){
        [views addObjectsFromArray:[self findAllSubviewsForView:subview]];
    }
    [views addObject:view];
    return views;
}

您可以使用此数组来查找UITextField,

UITextField * searchField = nil;
for(UIView * view in [self findAllSubviewsForView:self]){
     if([view isKindOfClass:[UITextField class]]){
          searchField = (UITextField *) view;
      }
 }