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

Dmi*_*try 7 uinavigationbar uinavigationcontroller uiprogressview ios swift

请问我如何在导航栏下添加ProgressView?我尝试在这篇文章中使用解决方案:在导航栏下添加进度条,但是代码是用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.
}

}
Run Code Online (Sandbox Code Playgroud)

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

我的错误在哪里?

Dmi*_*try 9

我的问题解决了.

  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)
    
    } 
    
    Run Code Online (Sandbox Code Playgroud)

    4.Success!