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 ?
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
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With