【问题标题】:Changing position of UIProgressView?改变 UIProgressView 的位置?
【发布时间】:2018-08-17 20:18:42
【问题描述】:

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

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

【问题讨论】:

标签: swift uiprogressview


【解决方案1】:

您可以使用 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])

【讨论】:

    【解决方案2】:

    这真的很简单。

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

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

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

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

    【讨论】:

    • 谢谢,这很有帮助!不过,我真的不想将它添加为子视图 - 我宁愿将它作为一个固定位置,恰好位于我屏幕上另一个对象的下方。 (上面的物体移动了,所以这只是开始。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多