【问题标题】:UINavigationController Toolbar - Adding status text with UIActivityIndicatorViewUINavigationController 工具栏 - 使用 UIActivityIndi​​catorView 添加状态文本
【发布时间】:2012-05-01 14:30:24
【问题描述】:

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

UIBarButtonItem *textFieldItem = [[[UIBarButtonItem alloc] initWithCustomView:textField] autorelease];
self.toolbarItems = [NSArray arrayWithObject:textFieldItem];

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

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

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

[self setToolbarItems:[NSMutableArray arrayWithObject:pvItem]];

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

【问题讨论】:

  • 你试过使用 UILabel 吗?
  • 可能是间距问题...添加一个灵活的空间项
  • 感谢您的建议。仍然只显示活动指示器。你觉得我的例子有什么问题吗?

标签: ios xcode uinavigationcontroller uibarbuttonitem uiactivityindicatorview


【解决方案1】:

不要将活动指示器和标签添加为单独的视图,而是创建一个包含它们的单个复合视图并将该复合视图添加到您的工具栏。

创建一个派生自 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];
}

您还必须公开用于 startAnimating、stopAnimating 和一个设置标签文本的方法,但希望您能明白这一点。

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

UIProgressViewWithLabel * pv = [[UIProgressViewWithLabel alloc] initWithFrame:CGRectMake(0,0,150,25)];

调整宽度以使其适合..

【讨论】:

  • 谢谢,这看起来正是我需要的,但我仍然无法让它显示出来。我根据您的回复更新了我的问题。
  • 如果我在进度视图上启动动画,它会显示出来,但仍然没有标签。
  • @Lee 我已经编辑了我的答案,在最后添加了一个额外的代码行,应该可以帮助你。
  • 完美!不知道我自己要花多长时间才能弄清楚。谢谢。
猜你喜欢
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多