From 071e627fe6130b3706fba9a56e2c880d7a5fe9f2 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 22 Aug 2023 16:05:36 -0500 Subject: [PATCH] refactored to use protocol Signed-off-by: Matt Bruce --- VDS.xcodeproj/project.pbxproj | 8 +- .../Icon/ButtonIcon/ButtonIcon.swift | 2 +- VDS/Components/Tabs/Tab.swift | 2 +- VDS/Components/Tabs/TabsContainer.swift | 2 +- .../UIView+NSLayoutConstraint.swift | 455 ------------------ .../LayoutConstraintable.swift} | 72 ++- 6 files changed, 56 insertions(+), 485 deletions(-) rename VDS/{Extensions/UILayoutGuide.swift => Protocols/LayoutConstraintable.swift} (91%) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 41fb8bb7..6aeb33a6 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -48,7 +48,7 @@ EA33623E2892EE950071C351 /* UIDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33623D2892EE950071C351 /* UIDevice.swift */; }; EA3362402892EF6C0071C351 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33623F2892EF6B0071C351 /* Label.swift */; }; EA33624728931B050071C351 /* Initable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33624628931B050071C351 /* Initable.swift */; }; - EA471F382A9545A700CE9E58 /* UILayoutGuide.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA471F372A9545A700CE9E58 /* UILayoutGuide.swift */; }; + EA471F3A2A95587500CE9E58 /* LayoutConstraintable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA471F392A95587500CE9E58 /* LayoutConstraintable.swift */; }; EA4DB18528CA967F00103EE3 /* SelectorGroupHandlerBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA4DB18428CA967F00103EE3 /* SelectorGroupHandlerBase.swift */; }; EA4DB2FD28D3D0CA00103EE3 /* AnyEquatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA4DB2FC28D3D0CA00103EE3 /* AnyEquatable.swift */; }; EA4DB30228DCBCA500103EE3 /* Badge.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA4DB30128DCBCA500103EE3 /* Badge.swift */; }; @@ -195,7 +195,7 @@ EA33623D2892EE950071C351 /* UIDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIDevice.swift; sourceTree = ""; }; EA33623F2892EF6B0071C351 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = ""; }; EA33624628931B050071C351 /* Initable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initable.swift; sourceTree = ""; }; - EA471F372A9545A700CE9E58 /* UILayoutGuide.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILayoutGuide.swift; sourceTree = ""; }; + EA471F392A95587500CE9E58 /* LayoutConstraintable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutConstraintable.swift; sourceTree = ""; }; EA4DB18428CA967F00103EE3 /* SelectorGroupHandlerBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorGroupHandlerBase.swift; sourceTree = ""; }; EA4DB2FC28D3D0CA00103EE3 /* AnyEquatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyEquatable.swift; sourceTree = ""; }; EA4DB30128DCBCA500103EE3 /* Badge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Badge.swift; sourceTree = ""; }; @@ -469,7 +469,6 @@ EA3361A7288B23300071C351 /* UIColor.swift */, EA81410F2A127066004F60D2 /* UIColor+VDSColor.swift */, EA33623D2892EE950071C351 /* UIDevice.swift */, - EA471F372A9545A700CE9E58 /* UILayoutGuide.swift */, EAF7F0B6289C12A600B287F5 /* UITapGestureRecognizer.swift */, EA8E40902A7D3F6300934ED3 /* UIView+Accessibility.swift */, EAB5FED329267EB300998C17 /* UIView+NSLayoutConstraint.swift */, @@ -494,6 +493,7 @@ EAF7F0A1289AFB3900B287F5 /* Errorable.swift */, EA3361AE288B26310071C351 /* FormFieldable.swift */, EA33624628931B050071C351 /* Initable.swift */, + EA471F392A95587500CE9E58 /* LayoutConstraintable.swift */, EA985C7C297DAED300F2FF2E /* Primitive.swift */, EAF7F0A5289B0CE000B287F5 /* Resetable.swift */, EA3361C8289054C50071C351 /* Surfaceable.swift */, @@ -954,7 +954,6 @@ EA5F86D02A1F936100BC83E4 /* TabsContainer.swift in Sources */, EAF7F0B1289B177F00B287F5 /* ColorLabelAttribute.swift in Sources */, EAC9258F2911C9DE00091998 /* EntryField.swift in Sources */, - EA471F382A9545A700CE9E58 /* UILayoutGuide.swift in Sources */, EAB1D2EA28AE84AA00DAE764 /* UIControlPublisher.swift in Sources */, EAD068922A560B65002E3A2D /* LoaderViewController.swift in Sources */, EABFEB642A26473700C4C106 /* NSAttributedString.swift in Sources */, @@ -967,6 +966,7 @@ EA0D1C372A681CCE00E5C127 /* ToggleView.swift in Sources */, EAF7F0B9289C139800B287F5 /* ColorConfiguration.swift in Sources */, EA3361BD288B2C760071C351 /* TypeAlias.swift in Sources */, + EA471F3A2A95587500CE9E58 /* LayoutConstraintable.swift in Sources */, EAB1D2CF28ABEF2B00DAE764 /* Typography.swift in Sources */, EA0D1C3B2A6AD51B00E5C127 /* Typogprahy+Styles.swift in Sources */, EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */, diff --git a/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift b/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift index f3e14b9f..63e8000a 100644 --- a/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift +++ b/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift @@ -239,7 +239,7 @@ open class ButtonIcon: Control { layoutGuideHeightConstraint = iconLayoutGuide.height(constant: size.containerSize) //pin layout guide - iconLayoutGuide.pinToOwningView() + iconLayoutGuide.pinToSuperView() //determines the center point of the icon centerXConstraint = icon.centerXAnchor.constraint(equalTo: iconLayoutGuide.centerXAnchor, constant: 0) diff --git a/VDS/Components/Tabs/Tab.swift b/VDS/Components/Tabs/Tab.swift index d21113ac..83bdb665 100644 --- a/VDS/Components/Tabs/Tab.swift +++ b/VDS/Components/Tabs/Tab.swift @@ -132,7 +132,7 @@ extension Tabs { isAccessibilityElement = true //pin layoutguide - layoutGuide.pinToOwningView() + layoutGuide.pinToSuperView() //pin trailing label.pinTrailing(layoutGuide.trailingAnchor) diff --git a/VDS/Components/Tabs/TabsContainer.swift b/VDS/Components/Tabs/TabsContainer.swift index 5a48e6ea..58745a5b 100644 --- a/VDS/Components/Tabs/TabsContainer.swift +++ b/VDS/Components/Tabs/TabsContainer.swift @@ -132,7 +132,7 @@ open class TabsContainer: View { stackView.addArrangedSubview(tabMenu) stackView.addArrangedSubview(contentView) - tabMenuLayoutGuide.pinToOwningView() + tabMenuLayoutGuide.pinToSuperView() } /// Function used to make changes to the View based off a change events or from local properties. diff --git a/VDS/Extensions/UIView+NSLayoutConstraint.swift b/VDS/Extensions/UIView+NSLayoutConstraint.swift index 229492e1..e53dc1d5 100644 --- a/VDS/Extensions/UIView+NSLayoutConstraint.swift +++ b/VDS/Extensions/UIView+NSLayoutConstraint.swift @@ -10,466 +10,11 @@ import UIKit import VDSFormControlsTokens extension UIView { - public func constraint(with identifier: String) -> NSLayoutConstraint? { return constraints.first { $0.identifier == identifier } } } -//-------------------------------------------------- -// MARK: - Pinning -//-------------------------------------------------- -extension UIView { - @discardableResult - /// Pins each to the all 4 anchor points to a view. - /// - Parameters: - /// - view: View that you will be pinned within. - /// - edges: Insets for each side. - /// - Returns: Yourself. - public func pin(_ view: UIView, with edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self { - pinLeading(view.leadingAnchor, edges.left) - pinTrailing(view.trailingAnchor, edges.right) - pinTop(view.topAnchor, edges.top) - pinBottom(view.bottomAnchor, edges.bottom) - return self - } - - @discardableResult - - /// Pins each to the all 4 anchor points to the view you are set within. - /// - Parameter edges: Insets for each side. - /// - Returns: Yourself. - public func pinToSuperView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self { - if let superview { - pin(superview, with: edges) - } - return self - } -} - -//-------------------------------------------------- -// MARK: - HeightAnchor -//-------------------------------------------------- -extension UIView { - - @discardableResult - /// Adds a heightAnchor. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func height(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - height(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a heightAnchor where the height constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func heightGreaterThanEqualTo(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - heightGreaterThanEqualTo(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a heightAnchor where the height constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func heightLessThanEqualTo(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - heightLessThanEqualTo(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a heightAnchor for the constant passed into the method. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func height(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - heightAnchor.constraint(equalToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a heightAnchor where the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func heightGreaterThanEqualTo(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - heightAnchor.constraint(greaterThanOrEqualToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a heightAnchor where the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func heightLessThanEqualTo(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - heightAnchor.constraint(lessThanOrEqualToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } - -} - -//-------------------------------------------------- -// MARK: - WidthAnchor -//-------------------------------------------------- -extension UIView { - - @discardableResult - /// Adds a widthAnchor. - /// - Parameter constant: Width Constant size. - /// - Returns: Yourself. - public func width(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - width(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a widthAnchor where the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func widthGreaterThanEqualTo(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - widthGreaterThanEqualTo(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a widthAnchor where the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func widthLessThanEqualTo(_ constant: CGFloat, _ priority: UILayoutPriority = .required) -> Self { - widthLessThanEqualTo(constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a widthAnchor for the constant passed into the method. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func width(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - widthAnchor.constraint(equalToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a widthAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func widthGreaterThanEqualTo(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - widthAnchor.constraint(greaterThanOrEqualToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a widthAnchor with the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter constant: Constant size. - /// - Returns: The Constraint that was created. - public func widthLessThanEqualTo(constant: CGFloat, priority: UILayoutPriority = .required) -> NSLayoutConstraint { - widthAnchor.constraint(lessThanOrEqualToConstant: constant).with { $0.priority = priority; $0.isActive = true } - } -} - -//-------------------------------------------------- -// MARK: - TopAnchor -//-------------------------------------------------- -extension UIView { - - @discardableResult - /// Adds a topAnchor. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func pinTop(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - return pinTop(nil, constant, priority) - } - - @discardableResult - /// Adds a topAnchor to a specific YAxisAnchor. - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTop(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTop(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a topAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTopLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTopLessThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a topAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTopGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTopGreaterThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a topAnchor for the constant passed into the method. - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTop(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor - guard let found else { return nil } - return topAnchor.constraint(equalTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a topAnchor with the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTopLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor - guard let found else { return nil } - return topAnchor.constraint(lessThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a topAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the topAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTopGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor - guard let found else { return nil } - return topAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } - -} - -//-------------------------------------------------- -// MARK: - BottomAnchor -//-------------------------------------------------- -extension UIView { - @discardableResult - /// Adds a bottomAnchor. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func pinBottom(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - return pinBottom(nil, constant, priority) - } - - @discardableResult - /// Adds a bottomAnchor to a specific YAxisAnchor. - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinBottom(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinBottom(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a bottomAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinBottomLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinBottomLessThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a bottomAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinBottomGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinBottomGreaterThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a bottomAnchor for the constant passed into the method. - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinBottom(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor - guard let found else { return nil } - return bottomAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a bottomAnchor with the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinBottomLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor - guard let found else { return nil } - return bottomAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a bottomAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the bottomAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinBottomGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor - guard let found else { return nil } - return bottomAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } -} - -//-------------------------------------------------- -// MARK: - LeadingAnchor -//-------------------------------------------------- -extension UIView { - - @discardableResult - /// Adds a leadingAnchor. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func pinLeading(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - return pinLeading(nil, constant, priority) - } - - @discardableResult - /// Adds a leadingAnchor to a specific XAxisAnchor. - /// - Parameter anchor:The anchor in which to attach the leadingAnchor. - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinLeading(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinLeading(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a leadingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the leadingAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinLeadingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinLeadingLessThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a leadingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the leadingAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinLeadingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinLeadingGreaterThanOrEqualTo(anchor: anchor, constant: constant, priority: priority) - return self - } - - @discardableResult - /// Adds a leadingAnchor for the constant passed into the method. - /// - Parameter anchor:The anchor in which to attach the leadingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinLeading(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor - guard let found else { return nil } - return leadingAnchor.constraint(equalTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a leadingAnchor with the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the leadingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinLeadingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor - guard let found else { return nil } - return leadingAnchor.constraint(lessThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a leadingAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the leadingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinLeadingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor - guard let found else { return nil } - return leadingAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } - } -} - -//-------------------------------------------------- -// MARK: - TrailingAnchor -//-------------------------------------------------- -extension UIView { - - @discardableResult - /// Adds a trailingAnchor. - /// - Parameter constant: Constant size. - /// - Returns: Yourself. - public func pinTrailing(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTrailing(nil, constant) - } - - @discardableResult - /// Adds a trailingAnchor to a specific XAxisAnchor. - /// - Parameter anchor:The anchor in which to attach the trailingAnchor. - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTrailing(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTrailing(anchor: anchor, constant: constant) - return self - } - - @discardableResult - /// Adds a trailingAnchor to a specific XAxisAnchor passed in using a lessThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the trailingAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTrailingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { - pinTrailingLessThanOrEqualTo(anchor: anchor, constant: constant) - return self - } - - @discardableResult - /// Adds a trailingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint - /// - Parameter anchor:The anchor in which to attach the trailingAnchor - /// - constant: Constant size. - /// - Returns: Yourself. - public func pinTrailingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self { - pinTrailingGreaterThanOrEqualTo(anchor: anchor, constant: constant) - return self - } - - @discardableResult - /// Adds a trailingAnchor for the constant passed into the method. - /// - Parameter anchor:The anchor in which to attach the trailingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTrailing(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor - guard let found else { return nil } - return trailingAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a trailingAnchor with the constant passed in using a lessThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the trailingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTrailingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor - guard let found else { return nil } - return trailingAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } - - @discardableResult - /// Adds a trailingAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. - /// - Parameter anchor:The anchor in which to attach the trailingAnchor - /// - constant: Constant size. - /// - Returns: The Constraint that was created. - public func pinTrailingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor - guard let found else { return nil } - return trailingAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } - } -} - extension NSLayoutConstraint { @discardableResult diff --git a/VDS/Extensions/UILayoutGuide.swift b/VDS/Protocols/LayoutConstraintable.swift similarity index 91% rename from VDS/Extensions/UILayoutGuide.swift rename to VDS/Protocols/LayoutConstraintable.swift index 7dc638db..ceededbc 100644 --- a/VDS/Extensions/UILayoutGuide.swift +++ b/VDS/Protocols/LayoutConstraintable.swift @@ -1,5 +1,5 @@ // -// NSLayoutGuide.swift +// LayoutConstraintable.swift // VDS // // Created by Matt Bruce on 8/22/23. @@ -7,12 +7,27 @@ import Foundation import UIKit +import VDSFormControlsTokens +public protocol LayoutConstraintable { + var superview: UIView? { get } + + var leadingAnchor: NSLayoutXAxisAnchor { get } + var trailingAnchor: NSLayoutXAxisAnchor { get } + var topAnchor: NSLayoutYAxisAnchor { get } + var bottomAnchor: NSLayoutYAxisAnchor { get } + + var widthAnchor: NSLayoutDimension { get } + var heightAnchor: NSLayoutDimension { get } + + var centerXAnchor: NSLayoutXAxisAnchor { get } + var centerYAnchor: NSLayoutYAxisAnchor { get } +} //-------------------------------------------------- // MARK: - Pinning //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Pins each to the all 4 anchor points to a view. /// - Parameters: @@ -32,9 +47,9 @@ extension UILayoutGuide { /// Pins each to the all 4 anchor points to the view you are set within. /// - Parameter edges: Insets for each side. /// - Returns: Yourself. - public func pinToOwningView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self { - if let owningView { - pin(owningView, with: edges) + public func pinToSuperView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self { + if let superview { + pin(superview, with: edges) } return self } @@ -43,7 +58,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - HeightAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a heightAnchor. @@ -101,7 +116,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - WidthAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a widthAnchor. @@ -158,7 +173,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - TopAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a topAnchor. @@ -204,7 +219,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTop(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.topAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor guard let found else { return nil } return topAnchor.constraint(equalTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -215,7 +230,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTopLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.topAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor guard let found else { return nil } return topAnchor.constraint(lessThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -226,7 +241,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTopGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.topAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor guard let found else { return nil } return topAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -236,7 +251,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - BottomAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a bottomAnchor. /// - Parameter constant: Constant size. @@ -281,7 +296,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinBottom(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.bottomAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor guard let found else { return nil } return bottomAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } @@ -292,7 +307,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinBottomLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.bottomAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor guard let found else { return nil } return bottomAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } @@ -303,7 +318,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinBottomGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutYAxisAnchor? = anchor ?? owningView?.bottomAnchor + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor guard let found else { return nil } return bottomAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } @@ -312,7 +327,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - LeadingAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a leadingAnchor. @@ -358,7 +373,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinLeading(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.leadingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor guard let found else { return nil } return leadingAnchor.constraint(equalTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -369,7 +384,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinLeadingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.leadingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor guard let found else { return nil } return leadingAnchor.constraint(lessThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -380,7 +395,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinLeadingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.leadingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor guard let found else { return nil } return leadingAnchor.constraint(greaterThanOrEqualTo: found, constant: constant).with { $0.priority = priority; $0.isActive = true } } @@ -389,7 +404,7 @@ extension UILayoutGuide { //-------------------------------------------------- // MARK: - TrailingAnchor //-------------------------------------------------- -extension UILayoutGuide { +extension LayoutConstraintable { @discardableResult /// Adds a trailingAnchor. @@ -435,7 +450,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTrailing(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.trailingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor guard let found else { return nil } return trailingAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } @@ -446,7 +461,7 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTrailingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.trailingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor guard let found else { return nil } return trailingAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } @@ -457,8 +472,19 @@ extension UILayoutGuide { /// - constant: Constant size. /// - Returns: The Constraint that was created. public func pinTrailingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { - let found: NSLayoutXAxisAnchor? = anchor ?? owningView?.trailingAnchor + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor guard let found else { return nil } return trailingAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } } } + + +//-------------------------------------------------- +// MARK: - Implementations +//-------------------------------------------------- +extension UIView: LayoutConstraintable {} +extension UILayoutGuide: LayoutConstraintable { + public var superview: UIView? { + owningView + } +}