导航栏中心的活动指示器

时间:2013-04-17 09:56:30

标签: ios xcode uinavigationcontroller uinavigationbar

在我的应用程序中,我希望在导航栏的中心添加活动指示器(标题位置)。当Web服务响应完成时,它应该替换为旧标题。我的应用程序中有5个导航栏。当我在谷歌中搜索时有几个代码,但他们只是更改左或右栏按钮。任何帮助?

4 个答案:

答案 0 :(得分:19)

使用导航项的titleView属性替换导航栏的标题。因此,要添加活动指标,请执行以下操作:

    UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    aiView.hidesWhenStopped = NO; //I added this just so I could see it
    self.navigationItem.titleView = aiView;

如果要删除它,并再次显示标题:

    self.navigationItem.titleView = nil;

答案 1 :(得分:11)

pasqls的回答对我很有用,我用快速写的

    func showActivityIndicator() {
        let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
        activityIndicatorView.frame = CGRectMake(0, 0, 14, 14)
        activityIndicatorView.color = UIColor.blackColor()
        activityIndicatorView.startAnimating()

        let titleLabel = UILabel.new()
        titleLabel.text = "...Connecting"
        titleLabel.font = UIFont.italicSystemFontOfSize(14)

        let fittingSize = titleLabel.sizeThatFits(CGSizeMake(200.0, activityIndicatorView.frame.size.height))
        titleLabel.frame = CGRectMake(activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8, activityIndicatorView.frame.origin.y, fittingSize.width, fittingSize.height)

        let titleView = UIView(frame: CGRectMake(((activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width) / 2), ((activityIndicatorView.frame.size.height) / 2), (activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width), (activityIndicatorView.frame.size.height)))
        titleView.addSubview(activityIndicatorView)
        titleView.addSubview(titleLabel)

        self.navigationItem.titleView = titleView
    }

    func hideActivityIndicator() {
        self.navigationItem.titleView = nil
    }

答案 2 :(得分:2)

此外,如果您想在活动指示器旁边添加文本标签(如Apple在设置应用中所做的那样,例如Facebook登录),您可以这样做:

- (void)showActivityIndicator
{
    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicatorView.frame = CGRectMake(0, 0, 22, 22);
    activityIndicatorView.color = [UIColor blackColor];
    [activityIndicatorView startAnimating];

    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"Creating Account";
    titleLabel.font = [UIFont boldFlatFontOfSize:18];

    CGSize fittingSize = [titleLabel sizeThatFits:CGSizeMake(200.0f, activityIndicatorView.frame.size.height)];
    titleLabel.frame = CGRectMake(activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8,
                                  activityIndicatorView.frame.origin.y,
                                  fittingSize.width,
                                  fittingSize.height);

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(-(activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width)/2,
                                                                 -(activityIndicatorView.frame.size.height)/2,
                                                                 activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width,
                                                                 activityIndicatorView.frame.size.height)];
    [titleView addSubview:activityIndicatorView];
    [titleView addSubview:titleLabel];

    self.navigationItem.titleView = titleView;
}

- (void)hideActivityIndicator
{
    self.navigationItem.titleView = nil;
}

答案 3 :(得分:0)

Swift 4

of pasql

private func showActivityIndicator() {
    let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .white)
    activityIndicatorView.frame = CGRect(x: 0, y: 0, width: 14, height: 14)
    activityIndicatorView.color = .black
    activityIndicatorView.startAnimating()

    let titleLabel = UILabel()
    titleLabel.text = "...Connecting"
    titleLabel.font = UIFont.italicSystemFont(ofSize: 14)

    let fittingSize = titleLabel.sizeThatFits(CGSize(width: 200.0, height: activityIndicatorView.frame.size.height))
    titleLabel.frame = CGRect(x: activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8,
                              y: activityIndicatorView.frame.origin.y,
                              width: fittingSize.width,
                              height: fittingSize.height)

    let rect = CGRect(x: (activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width) / 2,
                      y: (activityIndicatorView.frame.size.height) / 2,
                      width: activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width,
                      height: activityIndicatorView.frame.size.height)
    let titleView = UIView(frame: rect)
    titleView.addSubview(activityIndicatorView)
    titleView.addSubview(titleLabel)

    navigationItem.titleView = titleView
}

private func hideActivityIndicator() {
    navigationItem.titleView = nil
}