Last active
August 25, 2021 12:32
-
-
Save thomsmed/4fe9e40d8a1de68dce66d9858a15a75d to your computer and use it in GitHub Desktop.
Useful UIView extensions
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+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