在Swift中在导航栏下添加进度条?

9
请告诉我如何在导航栏下面添加一个ProgressView? 我尝试使用此帖子中的解决方案: adding progress bar under navigation bar ,但是该代码是用ObjectiveC语言编写的…… 我试图将其翻译为Swift。这是我在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.
}

}

但是当我编译我的应用程序时,我没有在导航栏下看到进度视图。
我的错误在哪里?

你应该接受一个答案,让其他用户知道你的问题已经得到解答 - 即使是由你自己回答的。 - Devran Cosmo Uenal
当然可以!但是stackoverflow告诉我我需要在12小时后才能接受我的答案 :) - Dmitry
2个回答

10

我的问题已经解决。

  1. 将Progress View添加到View Controller中。(拖放)
  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!


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接