refactored even more
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
342357820f
commit
c8e9ab75c2
@ -10,101 +10,31 @@ import UIKit
|
|||||||
import VDSFormControlsTokens
|
import VDSFormControlsTokens
|
||||||
|
|
||||||
extension UIView {
|
extension UIView {
|
||||||
public enum ConstraintIdentifier: CustomStringConvertible {
|
|
||||||
case leading, trailing, top, bottom, height, width
|
|
||||||
case custom(String)
|
|
||||||
|
|
||||||
public var description: String {
|
|
||||||
switch self {
|
|
||||||
case .leading: return "leading"
|
|
||||||
case .trailing: return "trailing"
|
|
||||||
case .top: return "top"
|
|
||||||
case .bottom: return "bottom"
|
|
||||||
case .height: return "height"
|
|
||||||
case .width: return "width"
|
|
||||||
case .custom(let identifier): return identifier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum ConstraintType {
|
|
||||||
case equal, lessThanOrEqual, greaterThanOrEqual
|
|
||||||
}
|
|
||||||
|
|
||||||
public var _topConstraint: NSLayoutConstraint? { constraint(with: .top)}
|
|
||||||
public var _leadingConstraint: NSLayoutConstraint? { constraint(with: .leading)}
|
|
||||||
public var _trailingConstraint: NSLayoutConstraint? { constraint(with: .trailing)}
|
|
||||||
public var _bottomConstraint: NSLayoutConstraint? { constraint(with: .bottom)}
|
|
||||||
public var _widthConstraint: NSLayoutConstraint? { constraint(with: .width)}
|
|
||||||
public var _heightConstraint: NSLayoutConstraint? { constraint(with: .height)}
|
|
||||||
|
|
||||||
public func constraint(with identifier: ConstraintIdentifier) -> NSLayoutConstraint? {
|
|
||||||
return constraint(with: identifier.description)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func constraint(with identifier: String) -> NSLayoutConstraint? {
|
public func constraint(with identifier: String) -> NSLayoutConstraint? {
|
||||||
return constraints.first { $0.identifier == identifier }
|
return constraints.first { $0.identifier == identifier }
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeConstraint(edges: [UIRectEdge]) {
|
|
||||||
let topConstraint: NSLayoutConstraint? = constraint(with: .top)
|
|
||||||
let leadingConstraint: NSLayoutConstraint? = constraint(with: .leading)
|
|
||||||
let trailingConstraint: NSLayoutConstraint? = constraint(with: .trailing)
|
|
||||||
let bottomConstraint: NSLayoutConstraint? = constraint(with: .bottom)
|
|
||||||
|
|
||||||
edges.forEach { edge in
|
|
||||||
switch edge {
|
|
||||||
case .all:
|
|
||||||
if let leadingConstraint {
|
|
||||||
removeConstraint(leadingConstraint)
|
|
||||||
}
|
|
||||||
if let trailingConstraint {
|
|
||||||
removeConstraint(trailingConstraint)
|
|
||||||
}
|
|
||||||
if let topConstraint {
|
|
||||||
removeConstraint(topConstraint)
|
|
||||||
}
|
|
||||||
if let bottomConstraint {
|
|
||||||
removeConstraint(bottomConstraint)
|
|
||||||
}
|
|
||||||
case .left:
|
|
||||||
if let leadingConstraint {
|
|
||||||
removeConstraint(leadingConstraint)
|
|
||||||
}
|
|
||||||
case .right:
|
|
||||||
if let trailingConstraint {
|
|
||||||
removeConstraint(trailingConstraint)
|
|
||||||
}
|
|
||||||
case .top:
|
|
||||||
if let topConstraint {
|
|
||||||
removeConstraint(topConstraint)
|
|
||||||
}
|
|
||||||
case .bottom:
|
|
||||||
if let bottomConstraint {
|
|
||||||
removeConstraint(bottomConstraint)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Pinning
|
// MARK: - Pinning
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
public func pin(_ view: UIView, with edges: UIEdgeInsets = UIEdgeInsets.zero) {
|
@discardableResult
|
||||||
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: edges.left, identifier: ConstraintIdentifier.leading.description).isActive = true
|
public func pin(_ view: UIView, with edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self {
|
||||||
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -edges.right, identifier: ConstraintIdentifier.trailing.description).isActive = true
|
pinLeading(view.leadingAnchor, edges.left)
|
||||||
topAnchor.constraint(equalTo: view.topAnchor, constant: edges.top, identifier: ConstraintIdentifier.top.description).isActive = true
|
pinTrailing(view.trailingAnchor, edges.right)
|
||||||
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -edges.bottom, identifier: ConstraintIdentifier.bottom.description).isActive = true
|
pinTop(view.topAnchor, edges.top)
|
||||||
|
pinBottom(view.bottomAnchor, edges.bottom)
|
||||||
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
public func pinToSuperView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) {
|
@discardableResult
|
||||||
|
public func pinToSuperView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self {
|
||||||
if let superview {
|
if let superview {
|
||||||
pin(superview, with: edges)
|
pin(superview, with: edges)
|
||||||
}
|
}
|
||||||
|
return self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,38 +44,38 @@ extension UIView {
|
|||||||
extension UIView {
|
extension UIView {
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func height(_ constant: CGFloat, type: ConstraintType = .equal) -> Self {
|
public func height(_ constant: CGFloat) -> Self {
|
||||||
switch type {
|
height(constant: constant)
|
||||||
case .equal:
|
|
||||||
height(constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
heightLessThanEqualTo(constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
heightGreaterThanEqualTo(constant)
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
|
||||||
public func height(_ constant: CGFloat) -> Self {
|
|
||||||
heightAnchor.constraint(equalToConstant: constant, identifier: ConstraintIdentifier.height.description).isActive = true
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func heightGreaterThanEqualTo(_ constant: CGFloat) -> Self {
|
public func heightGreaterThanEqualTo(_ constant: CGFloat) -> Self {
|
||||||
heightAnchor.constraint(greaterThanOrEqualToConstant: constant, identifier: ConstraintIdentifier.height.description).isActive = true
|
heightGreaterThanEqualTo(constant: constant)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func heightLessThanEqualTo(_ constant: CGFloat) -> Self {
|
public func heightLessThanEqualTo(_ constant: CGFloat) -> Self {
|
||||||
heightAnchor.constraint(lessThanOrEqualToConstant: constant, identifier: ConstraintIdentifier.height.description).isActive = true
|
heightLessThanEqualTo(constant: constant)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func height(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
heightAnchor.constraint(equalToConstant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func heightGreaterThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
heightAnchor.constraint(greaterThanOrEqualToConstant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func heightLessThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
heightAnchor.constraint(lessThanOrEqualToConstant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -153,38 +83,38 @@ extension UIView {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
|
|
||||||
@discardableResult
|
|
||||||
public func width(_ constant: CGFloat, type: ConstraintType = .equal) -> Self {
|
|
||||||
switch type {
|
|
||||||
case .equal:
|
|
||||||
width(constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
widthLessThanEqualTo(constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
widthGreaterThanEqualTo(constant)
|
|
||||||
}
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func width(_ constant: CGFloat) -> Self {
|
public func width(_ constant: CGFloat) -> Self {
|
||||||
widthAnchor.constraint(equalToConstant: constant, identifier: ConstraintIdentifier.width.description).isActive = true
|
width(constant: constant)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func widthGreaterThanEqualTo(_ constant: CGFloat) -> Self {
|
public func widthGreaterThanEqualTo(_ constant: CGFloat) -> Self {
|
||||||
widthAnchor.constraint(greaterThanOrEqualToConstant: constant, identifier: ConstraintIdentifier.width.description).isActive = true
|
widthGreaterThanEqualTo(constant: constant)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func widthLessThanEqualTo(_ constant: CGFloat) -> Self {
|
public func widthLessThanEqualTo(_ constant: CGFloat) -> Self {
|
||||||
widthAnchor.constraint(lessThanOrEqualToConstant: constant, identifier: ConstraintIdentifier.width.description).isActive = true
|
widthLessThanEqualTo(constant: constant)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func width(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
widthAnchor.constraint(equalToConstant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func widthGreaterThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
widthAnchor.constraint(greaterThanOrEqualToConstant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func widthLessThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
|
||||||
|
widthAnchor.constraint(lessThanOrEqualToConstant: constant).activate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -192,21 +122,6 @@ extension UIView {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
|
|
||||||
@discardableResult
|
|
||||||
public func pinTop(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ type: ConstraintType = .equal) -> Self {
|
|
||||||
switch type {
|
|
||||||
case .equal:
|
|
||||||
pinTop(anchor, constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
pinTopLessThanOrEqualTo(anchor, constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
pinTopGreaterThanOrEqualTo(anchor, constant)
|
|
||||||
}
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTop(_ constant: CGFloat = 0.0) -> Self {
|
public func pinTop(_ constant: CGFloat = 0.0) -> Self {
|
||||||
return pinTop(nil, constant)
|
return pinTop(nil, constant)
|
||||||
@ -214,51 +129,49 @@ extension UIView {
|
|||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTop(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTop(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
pinTop(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
topAnchor.constraint(equalTo: found, constant: constant, identifier: ConstraintIdentifier.top.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTopLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTopLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
pinBottomLessThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
topAnchor.constraint(lessThanOrEqualTo: found, constant: constant, identifier: ConstraintIdentifier.top.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTopGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTopGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
pinTopGreaterThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
topAnchor.constraint(greaterThanOrEqualTo: found, constant: constant, identifier: ConstraintIdentifier.top.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTop(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return topAnchor.constraint(equalTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTopLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return topAnchor.constraint(lessThanOrEqualTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTopGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return topAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - BottomAnchor
|
// MARK: - BottomAnchor
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
@discardableResult
|
|
||||||
public func pinBottom(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ type: ConstraintType = .equal) -> Self {
|
|
||||||
switch type {
|
|
||||||
case .equal:
|
|
||||||
pinBottom(anchor, constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
pinBottomLessThanOrEqualTo(anchor, constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
pinBottomGreaterThanOrEqualTo(anchor, constant)
|
|
||||||
}
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinBottom(_ constant: CGFloat = 0.0) -> Self {
|
public func pinBottom(_ constant: CGFloat = 0.0) -> Self {
|
||||||
return pinBottom(nil, constant)
|
return pinBottom(nil, constant)
|
||||||
@ -266,50 +179,48 @@ extension UIView {
|
|||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinBottom(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinBottom(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
pinBottom(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
bottomAnchor.constraint(equalTo: found, constant: -constant, identifier: ConstraintIdentifier.bottom.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinBottomLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinBottomLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
pinBottomLessThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
bottomAnchor.constraint(lessThanOrEqualTo: found, constant: -constant, identifier: ConstraintIdentifier.bottom.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinBottomGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinBottomGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
pinBottomGreaterThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
bottomAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant, identifier: ConstraintIdentifier.bottom.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinBottom(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return bottomAnchor.constraint(equalTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinBottomLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return bottomAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinBottomGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return bottomAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - LeadingAnchor
|
// MARK: - LeadingAnchor
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
@discardableResult
|
|
||||||
public func pinLeading(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ type: ConstraintType = .equal) -> Self {
|
|
||||||
switch type {
|
|
||||||
case .equal:
|
|
||||||
pinLeading(anchor, constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
pinLeadingLessThanOrEqualTo(anchor, constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
pinLeadingGreaterThanOrEqualTo(anchor, constant)
|
|
||||||
}
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinLeading(_ constant: CGFloat = 0.0) -> Self {
|
public func pinLeading(_ constant: CGFloat = 0.0) -> Self {
|
||||||
@ -318,30 +229,42 @@ extension UIView {
|
|||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinLeading(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinLeading(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
pinLeading(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
leadingAnchor.constraint(equalTo: found, constant: constant, identifier: ConstraintIdentifier.leading.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinLeadingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinLeadingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
pinLeadingLessThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
leadingAnchor.constraint(lessThanOrEqualTo: found, constant: constant, identifier: ConstraintIdentifier.leading.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinLeadingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinLeadingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
pinLeadingGreaterThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
leadingAnchor.constraint(greaterThanOrEqualTo: found, constant: constant, identifier: ConstraintIdentifier.leading.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinLeading(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return leadingAnchor.constraint(equalTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinLeadingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return leadingAnchor.constraint(lessThanOrEqualTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinLeadingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return leadingAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).activate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -349,50 +272,74 @@ extension UIView {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
extension UIView {
|
extension UIView {
|
||||||
|
|
||||||
@discardableResult
|
|
||||||
public func pinTrailing(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ type: ConstraintType = .equal) -> Self {
|
|
||||||
switch type {
|
|
||||||
case .equal:
|
|
||||||
pinTrailing(anchor, constant)
|
|
||||||
|
|
||||||
case .lessThanOrEqual:
|
|
||||||
pinTrailingLessThanOrEqualTo(anchor, constant)
|
|
||||||
|
|
||||||
case .greaterThanOrEqual:
|
|
||||||
pinLeadingGreaterThanOrEqualTo(anchor, constant)
|
|
||||||
}
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTrailing(_ constant: CGFloat = 0.0) -> Self {
|
public func pinTrailing(_ constant: CGFloat = 0.0) -> Self {
|
||||||
return pinTrailing(nil, constant)
|
pinTrailing(nil, constant)
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTrailing(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTrailing(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
pinTrailing(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
trailingAnchor.constraint(equalTo: found, constant: -constant, identifier: ConstraintIdentifier.trailing.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTrailingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTrailingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
pinTrailingLessThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
trailingAnchor.constraint(lessThanOrEqualTo: found, constant: -constant, identifier: ConstraintIdentifier.trailing.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func pinTrailingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
public func pinTrailingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
|
||||||
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
pinTrailingGreaterThanOrEqualTo(anchor: anchor, constant: constant)
|
||||||
if let found {
|
|
||||||
trailingAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant, identifier: ConstraintIdentifier.trailing.description).isActive = true
|
|
||||||
}
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTrailing(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return trailingAnchor.constraint(equalTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTrailingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return trailingAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func pinTrailingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat) -> NSLayoutConstraint? {
|
||||||
|
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
|
||||||
|
guard let found else { return nil }
|
||||||
|
return trailingAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).activate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension NSLayoutConstraint {
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func activate() -> Self{
|
||||||
|
isActive = true
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
public func deactivate() -> Self{
|
||||||
|
isActive = false
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Container {
|
||||||
|
public var topConstraint: NSLayoutConstraint?
|
||||||
|
public var leadingConstraint: NSLayoutConstraint?
|
||||||
|
public var trailingConstraint: NSLayoutConstraint?
|
||||||
|
public var bottomConstraint: NSLayoutConstraint?
|
||||||
|
public var widthConstraint: NSLayoutConstraint?
|
||||||
|
public var heightConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
|
public init(){}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user