簡體   English   中英

更改UIProgressView的位置?

[英]Changing position of UIProgressView?

我在屏幕上添加了進度條。 我希望它在容器中水平居中,但我想將其移動到屏幕底部。 如何編輯第三行以更改其位置?

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

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

那真的很簡單。

如果要在另一個視圖上方添加子視圖,請使用以下功能

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

要在另一個視圖下添加子視圖,請使用此功能

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM