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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10399156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:09:28  来源:igfitidea点击:

UINavigationController Toolbar - Adding status text with UIActivityIndicatorView

iosxcodeuinavigationcontrolleruibarbuttonitemuiactivityindicatorview

提问by Lee

I would like to add a loading activity indicator to my app similar to the one in the mail app with status text to the right. I am using a UINavigationController, so I know I need to set the toolbarItems array on each view where I want it to be displayed. I can add the activity indicator and it does show up, but when I try to add the text field using the code below the text does not show. Is there a way to create a container programmatically that has both the status text and the UIActivityIndicatorView that will show up if added to the toolbarItems array.

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

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


UPDATE: I created a class derived from UIView based on the code from pdriegen.
I also added this code to viewDidLoad in my controller

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

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

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

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

Currently nothing shows up in the toolbar. What am I missing?

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

回答by pdriegen

Instead of adding the activityindicator and the label as separate views, create a single composite view that contains both of them and add that composite view to your toolbar.

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

Create a class that derives from UIView, override initWithFrame and add this code:

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

You'll also have to expose methods for startAnimating, stopAnimating and one to set the text of the label, but hopefully you get the idea.

您还必须公开 startAnimating、stopAnimating 和设置标签文本的方法,但希望您能理解。

To add it to your toolbar, initialize with the following:

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

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

Play around with the width to make it fit..

玩弄宽度以使其适合..