Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity indicator at the centre of Navigation Bar

In my application I want add activity indicator at the centre of navigation bar(title position).when web service response completed it should replace with the old title.I have 5 navigation bars in my application.When I searched in google I got several codes but they are simply changing the left or right bar button.Any help ?

like image 861
Jeff Avatar asked Apr 17 '13 09:04

Jeff


2 Answers

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
}
like image 179
Vyacheslav Avatar answered Oct 19 '22 22:10

Vyacheslav


pasqls answer worked well for me, i've written it in 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
    }
like image 28
Waffeln Avatar answered Oct 19 '22 23:10

Waffeln