Search code examples
swiftconstraintsinitcgrect

change the bottom anchor constraint to 50 pixels from the bottom


I want the imgView to have a gap of 50 from the bottom. I dont know how to get a gab at the bottm. I have tried to do bottomAnchor - 50 but that does not work it causes a compile error. It says its not allowed because this is a binary statement.What I tried to code is below.

  override init(frame: CGRect) {
      imgView.bottomAnchor.constraint(equalTo: bottomAnchor-50).isActive = true


}

Solution

  • You have missed syntax. Update your code as follow will fix your issue.

    imgView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50).isActive = true
    

    If you are writing this code inside Subclass of UIView then add following line of code.

    imgView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 50).isActive = true
    

    Set bottom constraint value to -50 or 50 based on your requirement.