To create a view and constraints programmatically in Swift, you can follow these steps:
main.swift207 chars4 linesmain.swift92 chars2 lines
main.swift348 chars7 linesThis creates constraints for the top, leading, trailing, and bottom edges of myView relative to the view that it was added to. The constant values can be adjusted to change the spacing between the edges of the views.
Note that Autolayout constraints can be defined with the isActive property or the NSLayoutConstraint.activate method, which can be a better programming practice.
This is a basic example and the actual constraints depend on what you put. You may need to use the bottomAnchor and trailingAnchor too.
gistlibby LogSnag