Created
August 20, 2018 14:56
-
-
Save itspolly/6df4e884a3df26aed1f811a6e2d30f96 to your computer and use it in GitHub Desktop.
An extension to add useful pinning tools to UIView.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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