autolayout programmatically
translatesAutoresizingMaskIntoConstraints = false
let constraints = [
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),
view.widthAnchor.constraint(equalToConstant: 100),
view.heightAnchor.constraint(equalTo: view.widthAnchor)
]
NSLayoutConstraint.activate(constraints)
NSLayoutConstraint.activate([
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),
view.widthAnchor.constraint(equalToConstant: 100),
view.heightAnchor.constraint(equalTo: view.widthAnchor)
])
view 에 가능한 anchor 변수
var bottomAnchor: NSLayoutYAxisAnchor
//A layout anchor representing the bottom edge of the view’s frame.
var centerXAnchor: NSLayoutXAxisAnchor
//A layout anchor representing the horizontal center of the view’s frame.
var centerYAnchor: NSLayoutYAxisAnchor
//A layout anchor representing the vertical center of the view’s frame.
var firstBaselineAnchor: NSLayoutYAxisAnchor
//A layout anchor representing the baseline for the topmost line of text in the view.
var heightAnchor: NSLayoutDimension
//A layout anchor representing the height of the view’s frame.
var lastBaselineAnchor: NSLayoutYAxisAnchor
//A layout anchor representing the baseline for the bottommost line of text in the view.
var leadingAnchor: NSLayoutXAxisAnchor
//A layout anchor representing the leading edge of the view’s frame.
var leftAnchor: NSLayoutXAxisAnchor
//A layout anchor representing the left edge of the view’s frame.
var rightAnchor: NSLayoutXAxisAnchor
//A layout anchor representing the right edge of the view’s frame.
var topAnchor: NSLayoutYAxisAnchor
//A layout anchor representing the top edge of the view’s frame.
var trailingAnchor: NSLayoutXAxisAnchor
//A layout anchor representing the trailing edge of the view’s frame.
var widthAnchor: NSLayoutDimension
//A layout anchor representing the width of the view’s frame.
Building Constraints
func constraint(equalTo: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as equal to another.
func constraint(equalTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as equal to another item’s attribute plus a constant offset.
func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as greater than or equal to another.
func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as greater than or equal to another item’s attribute plus a constant offset.
func constraint(lessThanOrEqualTo: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as less than or equal to another.
func constraint(lessThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint
//Returns a constraint that defines one item’s attribute as less than or equal to another item’s attribute plus a constant offset.
layout guide
layoutMarginsGuide: Set constraints and keep the layout margins as spacing
readableContentGuide: Constraints the width to a size that is easy for the user to read
safeAreaLayoutGuide: Represents the portion of your view that is unobscured by bars and other content
greenView.heightAnchor.constraint(equalTo: greenView.widthAnchor, multiplier: 16/9).isActive = true