How do I create Auto Layout constraints in code in Swift?

Creating Auto Layout constraints in code in Swift allows developers to programmatically define the layout of user interface elements. Here’s an example of how to set up Auto Layout constraints for a simple view.

let myView = UIView() myView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(myView) NSLayoutConstraint.activate([ myView.centerXAnchor.constraint(equalTo: view.centerXAnchor), myView.centerYAnchor.constraint(equalTo: view.centerYAnchor), myView.widthAnchor.constraint(equalToConstant: 100), myView.heightAnchor.constraint(equalToConstant: 100) ])

Swift Auto Layout Constraints iOS Development