11

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

4

4 回答 4

20

您使用导航项的 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;
于 2013-04-17T15:51:02.470 回答
12

pasqls 答案对我来说效果很好,我已经用 swift 写了

    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
    }
于 2015-07-16T13:14:52.460 回答
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;
}
于 2015-04-22T16:03:53.647 回答
2

斯威夫特 4

的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
}
于 2018-06-06T10:39:19.177 回答