Allow pinning to a view with padding
This commit is contained in:
parent
dd95724d14
commit
4014fb41f1
|
@ -0,0 +1,14 @@
|
|||
//
|
||||
// UIEdgeInsets.swift
|
||||
//
|
||||
//
|
||||
// Created by Jed Fox on 2022-11-24.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIEdgeInsets {
|
||||
public static func constant(_ offset: CGFloat) -> Self {
|
||||
UIEdgeInsets(top: offset, left: offset, bottom: offset, right: offset)
|
||||
}
|
||||
}
|
|
@ -48,20 +48,19 @@ extension UIView {
|
|||
}
|
||||
|
||||
public extension UIView {
|
||||
|
||||
@discardableResult
|
||||
func pinToParent() -> [NSLayoutConstraint] {
|
||||
pinTo(to: self.superview)
|
||||
func pinToParent(padding: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
|
||||
pinTo(to: self.superview, padding: padding)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func pinTo(to view: UIView?) -> [NSLayoutConstraint] {
|
||||
func pinTo(to view: UIView?, padding: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
|
||||
guard let pinToView = view else { return [] }
|
||||
let constraints = [
|
||||
topAnchor.constraint(equalTo: pinToView.topAnchor),
|
||||
leadingAnchor.constraint(equalTo: pinToView.leadingAnchor),
|
||||
trailingAnchor.constraint(equalTo: pinToView.trailingAnchor),
|
||||
bottomAnchor.constraint(equalTo: pinToView.bottomAnchor),
|
||||
topAnchor.constraint(equalTo: pinToView.topAnchor, constant: padding.top),
|
||||
leadingAnchor.constraint(equalTo: pinToView.leadingAnchor, constant: padding.left),
|
||||
trailingAnchor.constraint(equalTo: pinToView.trailingAnchor, constant: -padding.right),
|
||||
bottomAnchor.constraint(equalTo: pinToView.bottomAnchor, constant: -padding.bottom),
|
||||
]
|
||||
NSLayoutConstraint.activate(constraints)
|
||||
return constraints
|
||||
|
|
Loading…
Reference in New Issue