Created
September 11, 2020 08:35
-
-
Save kutan74/b85c1166916a95f6a12008c686e11c27 to your computer and use it in GitHub Desktop.
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
import UIKit | |
public struct AnchoredConstraints { | |
var top, leading, bottom, trailing, width, height: NSLayoutConstraint? | |
} | |
extension UIView { | |
@available(iOS 9.0, *) | |
@discardableResult | |
public func anchor(superView: UIView? = nil, | |
top: NSLayoutYAxisAnchor? = nil, | |
leading: NSLayoutXAxisAnchor? = nil, | |
bottom: NSLayoutYAxisAnchor? = nil, | |
trailing: NSLayoutXAxisAnchor? = nil, | |
padding: UIEdgeInsets = .zero, | |
size: CGSize = .zero) -> AnchoredConstraints { | |
if let superView = superView { | |
superView.addSubview(self) | |
} | |
translatesAutoresizingMaskIntoConstraints = false | |
var anchoredConstraints = AnchoredConstraints() | |
if let top = top { | |
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top) | |
} | |
if let leading = leading { | |
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left) | |
} | |
if let bottom = bottom { | |
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom) | |
} | |
if let trailing = trailing { | |
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right) | |
} | |
if size.width != 0 { | |
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width) | |
} | |
if size.height != 0 { | |
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height) | |
} | |
[anchoredConstraints.top, | |
anchoredConstraints.leading, | |
anchoredConstraints.bottom, | |
anchoredConstraints.trailing, | |
anchoredConstraints.width, | |
anchoredConstraints.height].forEach { $0?.isActive = true } | |
return anchoredConstraints | |
} | |
@available(iOS 9.0, *) | |
public func fillSuperview(superView: UIView? = nil, padding: UIEdgeInsets = .zero) { | |
if let superView = superView { | |
superView.addSubview(self) | |
} | |
translatesAutoresizingMaskIntoConstraints = false | |
if let superviewTopAnchor = superview?.topAnchor { | |
topAnchor.constraint(equalTo: superviewTopAnchor, constant: padding.top).isActive = true | |
} | |
if let superviewBottomAnchor = superview?.bottomAnchor { | |
bottomAnchor.constraint(equalTo: superviewBottomAnchor, constant: -padding.bottom).isActive = true | |
} | |
if let superviewLeadingAnchor = superview?.leadingAnchor { | |
leadingAnchor.constraint(equalTo: superviewLeadingAnchor, constant: padding.left).isActive = true | |
} | |
if let superviewTrailingAnchor = superview?.trailingAnchor { | |
trailingAnchor.constraint(equalTo: superviewTrailingAnchor, constant: -padding.right).isActive = true | |
} | |
} | |
@available(iOS 9.0, *) | |
public func centerInSuperview(size: CGSize = .zero) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let superviewCenterXAnchor = superview?.centerXAnchor { | |
centerXAnchor.constraint(equalTo: superviewCenterXAnchor).isActive = true | |
} | |
if let superviewCenterYAnchor = superview?.centerYAnchor { | |
centerYAnchor.constraint(equalTo: superviewCenterYAnchor).isActive = true | |
} | |
if size.width != 0 { | |
widthAnchor.constraint(equalToConstant: size.width).isActive = true | |
} | |
if size.height != 0 { | |
heightAnchor.constraint(equalToConstant: size.height).isActive = true | |
} | |
} | |
public func add<T: UIView>(_ view: T, then closure: ((T) -> Void)) { | |
self.addSubview(view) | |
closure(view) | |
} | |
} | |
extension UIEdgeInsets { | |
init(_ top: CGFloat = 0, _ left: CGFloat = 0, _ bottom: CGFloat = 0, _ right: CGFloat = 0) { | |
self.init(top: top, left: left, bottom: bottom, right: right) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment