更改UIProgressView的位置?

时间:2018-08-17 20:18:42

标签: swift uiprogressview

我在屏幕上添加了进度条。我希望它在容器中水平居中,但我想将其移动到屏幕底部。如何编辑第三行以更改其位置?

func addControls() {
    progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
    progressView?.center = self.view.center
    view.addSubview(progressView!)
}

2 个答案:

答案 0 :(得分:0)

您可以使用NSLayoutConstraints并执行类似的操作。第三行是您将进度栏放置在另一个对象之上或之下的位置。

    var otherObject = UIView()
    self.view.addSubview(progressView)
    let topConstraint = NSLayoutConstraint(item: progressView, attribute: .topMargin, relatedBy: .equal, toItem: otherObject, attribute: .bottomMargin, multiplier: 1, constant: 0)
    let leftConstraint = NSLayoutConstraint(item: progressView, attribute: .leftMargin, relatedBy: .equal, toItem: self.view, attribute: .leftMargin, multiplier: 1, constant: 0)
    let rightConstraint = NSLayoutConstraint(item: progressView, attribute: .rightMargin, relatedBy: .equal, toItem: self.view, attribute: .rightMargin, multiplier: 1, constant: 0)
    let bottomConstraint = NSLayoutConstraint(item: progressView, attribute: .bottomMargin, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
    self.view.addConstraints([topConstraint, leftConstraint, rightConstraint, bottomConstraint])

答案 1 :(得分:0)

那真的很简单。

如果要在另一个视图上方添加子视图,请使用this function

func insertSubview(_ view: UIView, aboveSubview siblingSubview: UIView)

要在另一个视图下添加子视图,请使用this function

func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView)