想要在UIBarButtonItem旁边添加UIActivityIndi​​cator

时间:2012-11-22 10:39:12

标签: iphone objective-c ios xcode

如何在右侧栏按钮项旁边添加活动指示器(CreateNew按钮)?

enter image description here

3 个答案:

答案 0 :(得分:4)

您可以使用的方法之一是使用一些自定义视图初始化条形按钮。

可以帮助您获得一些提示的条形码最小代码是

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
    [aView setBackgroundColor:[UIColor blackColor]];
    [[aView layer] setCornerRadius:8.0f];
    [[aView layer] setBorderColor:[UIColor whiteColor].CGColor];

    UIActivityIndicatorView* loadView  = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [loadView startAnimating];
    [aView addSubview:loadView];
    [loadView setFrame:CGRectMake(5, 5, 30, 30)];
    [loadView release];

    UIButton* aButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 2, 80, 35)];
    [aButton setImage:[UIImage imageNamed:@"btnLogin.png"] forState:UIControlStateNormal];
    [aView addSubview:aButton];
    [aButton release];


    UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:aView];
    [self.navigationItem setRightBarButtonItem:barButton];
    [aView release];
    [barButton release];

}

这是如果以编程方式执行,否则您也可以使用nib。以这种方式创建栏按钮看起来像这样 -

enter image description here

您可以使用[[UIBarButtonItem alloc] initWithCustomView:aView];方法查找更多选项。

希望它有所帮助!!

答案 1 :(得分:0)

为此尝试自定义UINavigationController类。

navigationBar 上添加活动指标,并从ViewController控制它。

答案 2 :(得分:0)

您可以使用以下代码添加活动指示器:

    //Create an instance of activity indicator view
    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    //set the initial property
    [activityIndicator stopAnimating];
    [activityIndicator hidesWhenStopped];

    //Create an instance of Bar button item with custome view which is of activity indicator
    UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    //Set the bar button the navigation bar
    [self navigationItem].rightBarButtonItem = barButton;

    //Memory clean up
    [activityIndicator release];
    [barButton release];

请参阅此link以获取详细代码。