0

I added a progress bar to my screen. I want it to be centered horizontally in my container, but I want to move it to the bottom of my screen. How do I edit the third line to change its position?

func addControls() {
    progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
    progressView?.center = self.view.center
    view.addSubview(progressView!)
}
edh
  • 7
  • 6
  • This will give you everything you need: https://developer.apple.com/documentation/uikit/uiview – Yevhen Aug 17 '18 at 20:35
  • Yes, I have that - I just don't know how to structure the argument for the y-axis – edh Aug 17 '18 at 20:45

2 Answers2

0

You could use NSLayoutConstraints and do something like this. The 3rd line being where you put the progress bar on top or beneath the other object.

    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])
Chris
  • 85
  • 10
0

That's really simple.

If you want to add a subview above another one, use this function:

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

To add a subview below another one, use this function:

func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView)
Vin Gazoil
  • 1,942
  • 2
  • 20
  • 24
  • Thanks, this is helpful! I don't really want to add it as a subview, though - I'd rather have it as a fixed location that just happens to be below another object on my screen. (The object above moves, so it's really just for the start.) – edh Aug 20 '18 at 14:22