0

This is the code I'm trying out:

    @IBOutlet weak var btnInfo: UIButton!

override func viewWillAppear(_ animated: Bool) {
    self.view.addConstraint(NSLayoutConstraint(item: self.view,
                                             attribute: .topMargin,
                                             relatedBy: .equal,
                                             toItem: btnInfo,
                                             attribute: .notAnAttribute,
                                             multiplier: 1,
                                             constant: 20))
    self.view.addConstraint(NSLayoutConstraint(item: self.view,
                                             attribute: .rightMargin,
                                             relatedBy: .equal,
                                             toItem: btnInfo,
                                             attribute: .notAnAttribute,
                                             multiplier: 1,
                                             constant: 30))
}

Trying to add .topMargin and .rightMargin constraints to get the btnInfo to appear 30 points from top and 30 from right margin. Please help.

as diu
  • 1,010
  • 15
  • 31
  • 1
    Use anchors! It will make your life easier. See [here](https://stackoverflow.com/questions/44074872/programatic-constraints-not-obeyed/44078258#44078258) – mfaani Jul 03 '17 at 20:46

1 Answers1

1

If interface builder is an option, interface builder works great for this type of functionality. Select the object you are wanting to add your margins to and set constant margins on the top and the right.

Here is a screen capture. enter image description here

AgnosticDev
  • 1,843
  • 2
  • 20
  • 36