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

时间:2015-03-11 23:55:40

标签: ios swift uinavigationcontroller uinavigationbar uiprogressview

请告诉我如何在导航栏下添加ProgressView? 我试着在这篇文章中使用解决方案: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。

我的错误在哪里?

2 个答案:

答案 0 :(得分: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)
    
    } 
    

    4.Success!

答案 1 :(得分:2)

查看http://www.appcoda.com/webkit-framework-intro/ - 在"显示进度"一部分。

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