Skip to content

Instantly share code, notes, and snippets.

@thomsmed
Last active August 25, 2021 12:32
Show Gist options
  • Save thomsmed/4fe9e40d8a1de68dce66d9858a15a75d to your computer and use it in GitHub Desktop.
Save thomsmed/4fe9e40d8a1de68dce66d9858a15a75d to your computer and use it in GitHub Desktop.
Useful UIView extensions
//
// UIView+Extensions.swift
//
import UIKit
extension UIView {
convenience init(autolayout: Bool) {
self.init()
translatesAutoresizingMaskIntoConstraints = !autolayout
}
func add(subviews: [UIView]) { subviews.forEach { self.addSubview($0) } }
func pinEdges(view: UIView, inset: UIEdgeInsets = UIEdgeInsets.zero) -> [NSLayoutConstraint] {
return [
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: inset.left),
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: inset.right),
topAnchor.constraint(equalTo: view.topAnchor, constant: inset.top),
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: inset.bottom)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment