UINavigationController工具栏 - 使用UIActivityIndi​​catorView添加状态文本

Lee*_*Lee 3 xcode uinavigationcontroller uibarbuttonitem uiactivityindicatorview ios

我想在我的应用程序中添加一个加载活动指示器,类似于邮件应用程序中的状态文本.我正在使用UINavigationController,所以我知道我需要在每个我希望它显示的视图上设置toolbarItems数组.我可以添加活动指示器,它确实显示,但当我尝试使用下面的代码添加文本字段时,文本不显示.有没有办法以编程方式创建一个具有状态文本和UIActivityIndi​​catorView的容器,如果将其添加到toolbarItems数组中,它将显示出来.

UIBarButtonItem *textFieldItem = [[[UIBarButtonItem alloc] initWithCustomView:textField] autorelease];
self.toolbarItems = [NSArray arrayWithObject:textFieldItem];
Run Code Online (Sandbox Code Playgroud)

更新:我根据来自pdriegen的代码创建了一个派生自UIView的类.
我还将此代码添加到我的控制器中的viewDidLoad

UIProgressViewWithLabel * pv = [[UIProgressViewWithLabel alloc] init];

UIBarButtonItem * pvItem = [[UIBarButtonItem alloc] initWithCustomView:pv];

[self setToolbarItems:[NSMutableArray arrayWithObject:pvItem]];
Run Code Online (Sandbox Code Playgroud)

目前工具栏中没有显示任何内容.我错过了什么?

pdr*_*gen 10

不是将activityindicator和标签添加为单独的视图,而是创建包含它们的单个复合视图,并将该复合视图添加到工具栏.

创建一个派生自UIView的类,覆盖initWithFrame并添加以下代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self configureView];
    }
    return self;
}

-(void)configureView{

    self.backgroundColor = [UIColor clearColor];

    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];        
    activityIndicator.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height );
    activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    activityIndicator.backgroundColor = [UIColor clearColor];

    [self addSubview:activityIndicator];

    CGFloat labelX = activityIndicator.bounds.size.width + 2;

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 0.0f, self.bounds.size.width - (labelX + 2), self.frame.size.height)];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    label.font = [UIFont boldSystemFontOfSize:12.0f];
    label.numberOfLines = 1;

    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"Loading..";

    [self addSubview:label];
}
Run Code Online (Sandbox Code Playgroud)

你还必须公开startAnimating,stopAnimating和一个设置标签文本的方法,但希望你能得到这个想法.

要将其添加到工具栏,请使用以下内容进行初始化:

UIProgressViewWithLabel * pv = [[UIProgressViewWithLabel alloc] initWithFrame:CGRectMake(0,0,150,25)];
Run Code Online (Sandbox Code Playgroud)

玩弄宽度使其适合..