user8105388
user8105388

Reputation:

Positioning constraints not having any effect

I am trying to code the label in the viewDidLoad function. The label is showing up but the code I am using now is not affecting the positioning. I am trying to code everything in the viewDidLoad function. You can see the screenshot below.

override func viewDidLoad() {
    super.viewDidLoad()

    let backbutton = UILabel()
    backbutton.backgroundColor = UIColor.orange
    backbutton.translatesAutoresizingMaskIntoConstraints = false
    backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
    backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
    backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
    backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
    view.addSubview(backbutton)
}

enter image description here

Upvotes: 4

Views: 96

Answers (3)

Kamran
Kamran

Reputation: 15258

This is how you can align the label into the center of the view,

let backbutton = UILabel()
view.addSubview(backbutton)

backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).

backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true

As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,

backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

enter image description here

Upvotes: 3

Mahgolsadat Fathi
Mahgolsadat Fathi

Reputation: 3325

Try using CGRect instead:

            let screenWidth = UIScreen.main.bounds.width
            let screenHeight = UIScreen.main.bounds.height
            let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
            let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
            let buttonWidth:CGFloat = 300
            let buttonHeight:CGFloat = 300
            backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)

instead of screen width/height you can also try to get the width of your super view using:

  self.view.frame.width 
  self.view.frame.height

Upvotes: 0

Pushp
Pushp

Reputation: 1060

Try this!!

 self.view.setNeedsUpdateConstraints()
    self.view.layoutIfNeeded()

Upvotes: 2

Related Questions