To center a UIView in Swift using AutoLayout, you can use the following code:
main.swift784 chars16 lines
This code creates a new UIView called myView
, adds it to the parent view, and sets its translatesAutoresizingMaskIntoConstraints
property to false to enable AutoLayout constraints. It then creates two constraints using the NSLayoutConstraint
class to center the view horizontally and vertically within the parent view. Finally, the constraints are added to the parent view using the addConstraints
method.
gistlibby LogSnag