// // AutoLayout.swift // AutoLayoutSwiftDemo // // Created by Johnhao on 2022/1/17. // import UIKit public class AutoLayoutSwift { private weak var view: UIView? public required init(view: UIView?) { self.view = view } @discardableResult public func remake() -> AutoLayoutSwift { view?.jh_removeAllConstraints() return self } } // MARK: - Compression/Hugging extension AutoLayoutSwift { @discardableResult public func compressionHorizontal(_ priority: UILayoutPriority) -> AutoLayoutSwift { view?.jh_compressionHorizontal = priority return self } @discardableResult public func compressionVertical(_ priority: UILayoutPriority) -> AutoLayoutSwift { view?.jh_compressionVertical = priority return self } @discardableResult public func huggingHorizontal(_ priority: UILayoutPriority) -> AutoLayoutSwift { view?.jh_huggingHorizontal = priority return self } @discardableResult public func huggingVertical(_ priority: UILayoutPriority) -> AutoLayoutSwift { view?.jh_huggingVertical = priority return self } } // MARK: - Collapse extension AutoLayoutSwift { @discardableResult public func collapsed(_ collapsed: Bool) -> AutoLayoutSwift { view?.jh_collapsed = collapsed return self } @discardableResult public func autoCollapse(_ isAuto: Bool) -> AutoLayoutSwift { view?.jh_autoCollapse = isAuto return self } @discardableResult public func hiddenCollapse(_ isHidden: Bool) -> AutoLayoutSwift { return self } } // MARK: - Axis extension AutoLayoutSwift { @discardableResult public func center() -> AutoLayoutSwift { view?.jh_alignCenterToSuperView() return self } @discardableResult public func centerX() -> AutoLayoutSwift { view?.jh_alignAxisToSuperView(.centerX) return self } @discardableResult public func centerY() -> AutoLayoutSwift { view?.jh_alignAxisToSuperView(.centerY) return self } @discardableResult public func center(_ otherView: UIView) -> AutoLayoutSwift { view?.jh_alignAxis(.centerX, to: otherView) view?.jh_alignAxis(.centerY, to: otherView) return self } @discardableResult public func centerX(_ otherView: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1.0) -> AutoLayoutSwift { view?.jh_alignAxis(.centerX, to: otherView, offset: offset, multiplier: multiplier) return self } @discardableResult public func centerY(_ otherView: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1.0) -> AutoLayoutSwift { view?.jh_alignAxis(.centerY, to: otherView, offset: offset, multiplier: multiplier) return self } } // MARK: - Edge extension AutoLayoutSwift { @discardableResult public func edges(_ insets: UIEdgeInsets = UIEdgeInsets.zero) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperView(insets) return self } @discardableResult public func edges(_ insets: UIEdgeInsets = .zero, excludingEdge: NSLayoutConstraint.Attribute) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperView(insets, excludingEdge: excludingEdge) return self } @discardableResult public func edges(all: CGFloat) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperView(UIEdgeInsets(top: all, left: all, bottom: all, right: all)) return self } @discardableResult public func edges(all: CGFloat, excludingEdge: NSLayoutConstraint.Attribute) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperView(UIEdgeInsets(top: all, left: all, bottom: all, right: all), excludingEdge: excludingEdge) return self } @discardableResult public func edgesHorzontal(_ inset: CGFloat = 0) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperViewHorizontal(inset: inset) return self } @discardableResult public func edgesVertical(_ inset: CGFloat = 0) -> AutoLayoutSwift { view?.jh_pinEdgesToSuperViewVertical(inset: inset) return self } @discardableResult public func top(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.top, inset: inset, relation: relation) return self } @discardableResult public func bottom(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.bottom, inset: inset, relation: relation) return self } @discardableResult public func left(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.leading, inset: inset, relation: relation) return self } @discardableResult public func leading(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.leading, inset: inset, relation: relation) return self } @discardableResult public func right(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.trailing, inset: inset, relation: relation) return self } @discardableResult public func trailing(_ inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdgeToSuperView(.trailing, inset: inset, relation: relation) return self } @discardableResult public func topToView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.top, toEdge: .top, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func bottomToView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.bottom, toEdge: .bottom, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func leftToView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.leading, toEdge: .leading, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func rightToView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.trailing, toEdge: .trailing, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func topToBottomOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.top, toEdge: .bottom, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func bottomToTopOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.bottom, toEdge: .top, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func leftToRightOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.leading, toEdge: .trailing, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func rightToLeftOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.trailing, toEdge: .leading, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func topToCenterYOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.top, toEdge: .centerY, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func bottomToCenterYOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.bottom, toEdge: .centerY, of: otherView, offset: -offset, relation: relation) return self } @discardableResult public func leftToCenterXOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.leading, toEdge: .centerX, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func rightToCenterXOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.trailing, toEdge: .centerX, of: otherView, offset: -offset, relation: relation) return self } @discardableResult public func centerXToRightOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.centerX, toEdge: .trailing, of: otherView, offset: offset, relation: relation) return self } @discardableResult public func centerYToTopOfView(_ otherView: UIView, offset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_pinEdge(.centerY, toEdge: .top, of: otherView, offset: offset, relation: relation) return self } } // MARK: - Baseline extension AutoLayoutSwift { @discardableResult public func firstBaselineToView(_ otherView: UIView, to attribute: NSLayoutConstraint.Attribute = .firstBaseline) -> AutoLayoutSwift { view?.jh_constrainAttribute(.firstBaseline, toAttribute: attribute, of: otherView) return self } @discardableResult public func lastBaselineToView(_ otherView: UIView, to attribute: NSLayoutConstraint.Attribute = .lastBaseline) -> AutoLayoutSwift { view?.jh_constrainAttribute(.lastBaseline, toAttribute: attribute, of: otherView) return self } @discardableResult public func constrainAttribute(_ attribute: NSLayoutConstraint.Attribute, otherView: UIView, to otherAttribute: NSLayoutConstraint.Attribute, offset: CGFloat = 0, multiplier: CGFloat = 1.0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_constraint(attribute, toAttribute: otherAttribute, otherView: otherView, offset: offset, multiplier: multiplier, relation: relation) return self } } // MARK: - Dimension extension AutoLayoutSwift { @discardableResult public func size(_ size: CGSize) -> AutoLayoutSwift { view?.jh_setDimensionToSize(size) return self } @discardableResult public func width(_ width: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_setDimension(.width, toSize: width, relation: relation) return self } @discardableResult public func height(_ height: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_setDimension(.height, toSize: height, relation: relation) return self } @discardableResult public func widthToHeight(_ multipler: CGFloat, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_matchDimension(.width, toDimension: .height, multiplier: multipler, relation: relation) return self } @discardableResult public func heightToWidth(_ multipler: CGFloat, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_matchDimension(.height, toDimension: .width, multiplier: multipler, relation: relation) return self } @discardableResult public func sizeToView(_ otherView: UIView) -> AutoLayoutSwift { view?.jh_matchDimension(.width, toDimension: .width, ofView: otherView) view?.jh_matchDimension(.height, toDimension: .height, ofView: otherView) return self } @discardableResult public func widthToView(_ otherView: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1.0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_matchDimension(.width, toDimension: .width, ofView: otherView, offset: offset, multiplier: multiplier,relation: relation) return self } @discardableResult public func heightToView(_ otherView: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1.0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_matchDimension(.height, toDimension: .height, ofView: otherView, offset: offset, multiplier: multiplier, relation: relation) return self } } // MARK: - Attribute extension AutoLayoutSwift { @discardableResult public func attribute(_ attribute: NSLayoutConstraint.Attribute, to otherArribute: NSLayoutConstraint.Attribute, of otherView: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1.0, relation: NSLayoutConstraint.Relation = .equal) -> AutoLayoutSwift { view?.jh_constraint(attribute, toAttribute: otherArribute, otherView: otherView, offset: offset, multiplier: multiplier, relation: relation) return self } } // MARK: - Constranit extension AutoLayoutSwift { public var constraint: NSLayoutConstraint? { view?.jh_lastConstraint } // @discardableResult // public func constraint(_ attribute: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation = .equal) -> NSLayoutConstraint? { // view?.jh_constrainAttribute(attribute, toAttribute: attribute, of: <#T##UIView#>) // } }