Skip to content

Instantly share code, notes, and snippets.

@itspolly
Created August 20, 2018 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itspolly/6df4e884a3df26aed1f811a6e2d30f96 to your computer and use it in GitHub Desktop.
Save itspolly/6df4e884a3df26aed1f811a6e2d30f96 to your computer and use it in GitHub Desktop.
An extension to add useful pinning tools to UIView.
//
// UIView+pin.swift
//
// Created by Jamie Bishop on 2018-07-20.
// Copyright © 2018 Jamie Bishop.
//
// Feel free to use this code without credit.
// https://gist.github.com/jamiebishop
import UIKit
extension UIView {
/// Pins this view to it's superview.
func pinToSuperview() {
guard let superview = superview else { fatalError("UIView+pinToSuperview: \(description) has no superview.") }
pin(to: superview)
}
/// Pins this view to another view.
func pin(to view: UIView) {
leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topAnchor.constraint(equalTo: view.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment