Question or problem in the Swift programming language:
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?
How to solve the problem:
Solution 1:
My problem was solved.
- Add the Progress View to View Controller.(Drag and drop)
- Make a IBOutlet.
-
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!
Solution 2:
Have a look at http://www.appcoda.com/webkit-framework-intro/ – below the “Displaying Progress” part.
It’s written in Swift, but they use interface builder to create the constraints.



