How to programmatically add Auto Layout constraints in Swift?
Hi everyone! I just started learning iOS development in Swift and I'm having trouble with programmatically adding constraints. I can work with Interface Builder more or less, but when I need to create UI in code — everything becomes difficult.
I have a simple screen: I create a UIView (let's call it `myView`) with a blue background and I want it to be 200x200 points and centered on the screen. Here's my current code:
```swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = .blue
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
// Trying to add constraints, but nothing
No answers yet.
Log in to leave an answer
Blogs