在 swift 的导航栏下添加进度条?

Adding progress bar under navigation bar in swift?

请问如何在导航栏下添加一个ProgressView? 我尝试在这个 post: adding progress bar under navigation bar 中使用解决方案,但是代码是用 ObjectiveC 语言编写的...我尝试翻译成 Swift。这是我在 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.
}

}

但是当我编译我的应用程序时,我没有在导航栏下看到 ProgressView。

我的错误在哪里?

查看 http://www.appcoda.com/webkit-framework-intro/ - "Displaying Progress" 部分下方。

它是用 Swift 编写的,但他们使用界面生成器来创建约束。

我的问题解决了。

  1. 将进度视图添加到视图控制器。(拖放)
  2. 创建一个 IBOutlet。
  3. 写代码:

    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!