3

右側にステータステキストが表示されたメールアプリと同様の読み込みアクティビティインジケーターをアプリに追加したいと思います。UINavigationControllerを使用しているので、表示する各ビューにtoolbarItems配列を設定する必要があることはわかっています。アクティビティインジケーターを追加すると表示されますが、下のコードを使用してテキストフィールドを追加しようとすると、テキストが表示されません。ステータステキストと、toolbarItems配列に追加された場合に表示されるUIActivityIndi​​catorViewの両方を持つコンテナをプログラムで作成する方法はありますか?

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]];

現在、ツールバーには何も表示されません。私は何が欠けていますか?

4

1 に答える 1

10

アクティビティインジケーターとラベルを別々のビューとして追加する代わりに、両方を含む単一の複合ビューを作成し、その複合ビューをツールバーに追加します。

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)];

幅を調整してフィットさせます。

于 2012-05-01T17:16:04.737 に答える