Skip to content

Instantly share code, notes, and snippets.

@orgmir
Created May 21, 2018 05:32
Show Gist options
  • Save orgmir/be30d553dfd779f8f2c0e142fbdde46e to your computer and use it in GitHub Desktop.
Save orgmir/be30d553dfd779f8f2c0e142fbdde46e to your computer and use it in GitHub Desktop.
Extensions for UIView that help out when building layouts manually!
//
// UIView+Extensions.swift
//
// Created by Luis Ramos on 17/5/18.
//
import UIKit
extension UIView {
var left: CGFloat {
get { return frame.origin.x }
set { frame = CGRect(x: newValue, y: frame.origin.y, width: frame.size.width, height: frame.size.height) }
}
var top: CGFloat {
get { return frame.origin.y }
set { frame = CGRect(x: frame.origin.x, y: newValue, width: frame.size.width, height: frame.size.height) }
}
var width: CGFloat {
get { return frame.size.width }
set { frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: newValue, height: frame.size.height) }
}
var height: CGFloat {
get { return frame.size.height }
set { frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: newValue) }
}
var right: CGFloat {
get { return left + width }
set { left = newValue - width }
}
var bottom: CGFloat {
get { return top + height }
set { top = newValue - height }
}
var centerX: CGFloat {
get { return left + width * 0.5 }
set { left = newValue - width * 0.5 }
}
var centerY: CGFloat {
get { return top + height * 0.5 }
set { top = newValue - height * 0.5 }
}
var relativeCenter : CGPoint {
return CGPoint(x: bounds.width / 2.0, y: bounds.height / 2.0)
}
var size: CGSize {
get { return frame.size }
set { frame.size = newValue }
}
}
extension UIView {
func align(withView view: UIView, constant: CGFloat = 0.0) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
leftAnchor.constraint(equalTo: view.leftAnchor, constant: constant),
rightAnchor.constraint(equalTo: view.rightAnchor, constant: -constant),
topAnchor.constraint(equalTo: view.topAnchor, constant: constant),
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -constant),
])
}
func alignLeadingTrailing(withView view: UIView, constant: CGFloat = 0.0) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
leftAnchor.constraint(equalTo: view.leftAnchor, constant: constant),
rightAnchor.constraint(equalTo: view.rightAnchor, constant: -constant),
])
}
func alignTopBottom(withView view: UIView, constant: CGFloat = 0.0) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
topAnchor.constraint(equalTo: view.topAnchor, constant: constant),
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -constant),
])
}
@discardableResult
func alignTop(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = topAnchor.constraint(equalTo: view.topAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func alignTop(withGuide guide: UILayoutGuide, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = topAnchor.constraint(equalTo: guide.topAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func alignBottom(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func alignBottom(withGuide guide: UILayoutGuide, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func alignLeading(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func alignTrailing(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constraintVerticalSpace(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = bottomAnchor.constraint(equalTo: view.topAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constraintHorizontalSpace(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = leadingAnchor.constraint(equalTo: view.trailingAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func centerVertical(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func centerHorizontal(withView view: UIView, constant: CGFloat = 0.0, isActive: Bool = true) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint = centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: constant)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constrainHeight(_ value: CGFloat, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = heightAnchor.constraint(equalToConstant: value)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constrainWidth(_ value: CGFloat, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = widthAnchor.constraint(equalToConstant: value)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constrainWidth(to view: UIView, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = widthAnchor.constraint(equalTo: view.widthAnchor)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constrainHeight(to view: UIView, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = heightAnchor.constraint(equalTo: view.heightAnchor)
constraint.isActive = isActive
return constraint
}
func constraintSize(width: CGFloat, height: CGFloat, isActive: Bool = true) {
widthAnchor.constraint(equalToConstant: width).isActive = true
heightAnchor.constraint(equalToConstant: height).isActive = true
}
@discardableResult
func constrainHeightToWidth(ratio: CGFloat = 1, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = heightAnchor.constraint(equalTo: widthAnchor, multiplier: ratio)
constraint.isActive = isActive
return constraint
}
@discardableResult
func constrainWidthToHeight(ratio: CGFloat = 1, isActive: Bool = true) -> NSLayoutConstraint {
let constraint = widthAnchor.constraint(equalTo: heightAnchor, multiplier: ratio)
constraint.isActive = isActive
return constraint
}
}
extension UIView {
var layoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide
} else {
return layoutMarginsGuide
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment