Adding a progress bar to the navigation bar in swift?

Please tell me how to add a ProgressView under the navigation bar? I am trying to use the solution in this post: add a progress bar in the navigation bar , but the code in ObjectiveC was written there ... I'm trying to translate to Swift. This is the code I added to my SubClass NavigationController

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 application, I do not see the ProgressView in the navigation bar.

Where is my mistake?

+7
ios swift uinavigationcontroller uinavigationbar uiprogressview
source share
2 answers

My problem is resolved.

  • Add a progress view to view the controller. (Drag and drop)
  • Make an IBOutlet.
  • Enter 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!

+7
source share

Take a look at http://www.appcoda.com/webkit-framework-intro/ - below the โ€œDisplay Progressโ€ section.

This is written in Swift, but they use an interface constructor to create constraints.

+2
source share

All Articles