Adding progress bar under navigation bar in swift?

Dmitry picture Dmitry · Mar 12, 2015 · Viewed 9k times · Source

Say me please how to add a ProgressView under navigation bar? I try to use solution in this post: adding progress bar under navigation bar, but there code was written on ObjectiveC language... I try to translate to Swift. This is the code, which i added in my NavigationController SubClass

import UIKit
class CustomNavigationController: UINavigationController {

@IBOutlet var Secondprogress: UIProgressView!
override func viewDidLoad() {
    super.viewDidLoad()



    NSLayoutConstraint.deactivateConstraints(self.view.constraints())

    Secondprogress?.setTranslatesAutoresizingMaskIntoConstraints(false)




    var navBar = self.navigationController?.navigationBar
    Secondprogress.tag = 1
    self.view.addSubview(Secondprogress)

    var Constraint = NSLayoutConstraint(item: self.Secondprogress,
        attribute:NSLayoutAttribute.Bottom,
        relatedBy:NSLayoutRelation.Equal,
        toItem:navBar,
        attribute:NSLayoutAttribute.Bottom,
        multiplier:1.0,
        constant:-0.5);

    self.view.addConstraint(Constraint);

    Constraint = NSLayoutConstraint(item: self.Secondprogress,
        attribute:NSLayoutAttribute.Left,
        relatedBy:NSLayoutRelation.Equal,
        toItem:navBar,
        attribute:NSLayoutAttribute.Left,
        multiplier:1.0,
        constant:0);

    self.view.addConstraint(Constraint);

    Constraint = NSLayoutConstraint(item: self.Secondprogress,
        attribute:NSLayoutAttribute.Right,
        relatedBy:NSLayoutRelation.Equal,
        toItem:navBar,
        attribute:NSLayoutAttribute.Right,
        multiplier:1.0,
        constant:0);

    self.view.addConstraint(Constraint);

    Secondprogress.setTranslatesAutoresizingMaskIntoConstraints(false)

    Secondprogress.hidden = false
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

But when i compile my app, i don't see the ProgressView under Navigation Bar.

Where is my mistake?

Answer

Dmitry picture Dmitry · Mar 12, 2015

My problem was solved.

  1. Add the Progress View to View Controller.(Drag and drop)
  2. Make a IBOutlet.
  3. Write the code:

    override func viewDidLoad() {
    super.viewDidLoad()
    var navBar = self.navigationController?.navigationBar
    var navBarHeight = navBar?.frame.height
    var ProgressFrame = self.Progress.frame
    var pSetX = ProgressFrame.origin.x
    var pSetY = CGFloat(navBarHeight!)
    var pSetWidth = self.view.frame.width
    var pSetHight = ProgressFrame.height
    
    Progress.frame = CGRectMake(pSetX, pSetY, pSetWidth, pSetHight)
    self.navigationController?.navigationBar.addSubview(Progress)
    Progress.setTranslatesAutoresizingMaskIntoConstraints(false)
    
    } 
    

    4.Success!