session-ios/SessionUIKit/Utilities/UIView+Constraints.swift

131 lines
5.1 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
2019-09-20 08:47:27 +02:00
2020-03-10 23:46:54 +01:00
public protocol ConstraintUtilitiesEdge { }
2020-01-20 03:20:27 +01:00
public extension UIView {
2019-09-20 08:47:27 +02:00
2020-03-10 23:46:54 +01:00
enum HorizontalEdge : ConstraintUtilitiesEdge { case left, leading, right, trailing }
enum VerticalEdge : ConstraintUtilitiesEdge { case top, bottom }
enum Direction { case horizontal, vertical }
enum Dimension { case width, height }
2019-09-20 08:47:27 +02:00
private func anchor(from edge: HorizontalEdge) -> NSLayoutXAxisAnchor {
switch edge {
case .left: return leftAnchor
case .leading: return leadingAnchor
case .right: return rightAnchor
case .trailing: return trailingAnchor
}
}
private func anchor(from edge: VerticalEdge) -> NSLayoutYAxisAnchor {
switch edge {
case .top: return topAnchor
case .bottom: return bottomAnchor
}
}
2019-11-20 04:18:38 +01:00
@discardableResult
2020-03-10 23:46:54 +01:00
func pin(_ constraineeEdge: HorizontalEdge, to constrainerEdge: HorizontalEdge, of view: UIView, withInset inset: CGFloat = 0) -> NSLayoutConstraint {
2019-09-20 08:47:27 +02:00
translatesAutoresizingMaskIntoConstraints = false
2019-11-20 04:18:38 +01:00
let constraint = anchor(from: constraineeEdge).constraint(equalTo: view.anchor(from: constrainerEdge), constant: inset)
constraint.isActive = true
return constraint
2019-09-20 08:47:27 +02:00
}
2019-11-20 04:18:38 +01:00
@discardableResult
2020-03-10 23:46:54 +01:00
func pin(_ constraineeEdge: VerticalEdge, to constrainerEdge: VerticalEdge, of view: UIView, withInset inset: CGFloat = 0) -> NSLayoutConstraint {
2019-09-20 08:47:27 +02:00
translatesAutoresizingMaskIntoConstraints = false
2019-11-20 04:18:38 +01:00
let constraint = anchor(from: constraineeEdge).constraint(equalTo: view.anchor(from: constrainerEdge), constant: inset)
constraint.isActive = true
return constraint
2019-09-20 08:47:27 +02:00
}
2020-03-10 23:46:54 +01:00
func pin(_ edges: [ConstraintUtilitiesEdge], to view: UIView) {
edges.forEach {
if let horizontalEdge = $0 as? HorizontalEdge {
pin(horizontalEdge, to: horizontalEdge, of: view)
} else if let verticalEdge = $0 as? VerticalEdge {
pin(verticalEdge, to: verticalEdge, of: view)
} else {
preconditionFailure() // Should never occur
}
}
}
func pin(to view: UIView) {
2019-10-09 05:46:21 +02:00
[ HorizontalEdge.leading, HorizontalEdge.trailing ].forEach { pin($0, to: $0, of: view) }
[ VerticalEdge.top, VerticalEdge.bottom ].forEach { pin($0, to: $0, of: view) }
}
2020-03-10 23:46:54 +01:00
func pin(to view: UIView, withInset inset: CGFloat) {
2019-12-06 04:42:43 +01:00
pin(.leading, to: .leading, of: view, withInset: inset)
pin(.top, to: .top, of: view, withInset: inset)
view.pin(.trailing, to: .trailing, of: self, withInset: inset)
view.pin(.bottom, to: .bottom, of: self, withInset: inset)
}
2019-11-20 04:18:38 +01:00
@discardableResult
2020-03-10 23:46:54 +01:00
func center(_ direction: Direction, in view: UIView) -> NSLayoutConstraint {
2019-09-20 08:47:27 +02:00
translatesAutoresizingMaskIntoConstraints = false
2019-11-20 04:18:38 +01:00
let constraint: NSLayoutConstraint = {
switch direction {
case .horizontal: return centerXAnchor.constraint(equalTo: view.centerXAnchor)
case .vertical: return centerYAnchor.constraint(equalTo: view.centerYAnchor)
}
}()
constraint.isActive = true
return constraint
2019-09-20 08:47:27 +02:00
}
2020-03-10 23:46:54 +01:00
func center(in view: UIView) {
2019-11-20 02:06:41 +01:00
center(.horizontal, in: view)
center(.vertical, in: view)
}
2019-11-20 04:18:38 +01:00
@discardableResult
2020-03-10 23:46:54 +01:00
func set(_ dimension: Dimension, to size: CGFloat) -> NSLayoutConstraint {
2019-09-20 08:47:27 +02:00
translatesAutoresizingMaskIntoConstraints = false
2019-11-20 04:18:38 +01:00
let constraint: NSLayoutConstraint = {
switch dimension {
case .width: return widthAnchor.constraint(equalToConstant: size)
case .height: return heightAnchor.constraint(equalToConstant: size)
}
}()
constraint.isActive = true
return constraint
2019-09-20 08:47:27 +02:00
}
@discardableResult
func set(_ dimension: Dimension, to otherDimension: Dimension, of view: UIView, withOffset offset: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let otherAnchor: NSLayoutDimension = {
switch otherDimension {
case .width: return view.widthAnchor
case .height: return view.heightAnchor
}
}()
let constraint: NSLayoutConstraint = {
switch dimension {
case .width: return widthAnchor.constraint(equalTo: otherAnchor, multiplier: multiplier, constant: offset)
case .height: return heightAnchor.constraint(equalTo: otherAnchor, multiplier: multiplier, constant: offset)
}
}()
constraint.isActive = true
return constraint
}
@discardableResult
func set(_ dimension: Dimension, greaterThanOrEqualTo size: CGFloat) -> NSLayoutConstraint {
translatesAutoresizingMaskIntoConstraints = false
let constraint: NSLayoutConstraint = {
switch dimension {
case .width: return widthAnchor.constraint(greaterThanOrEqualToConstant: size)
case .height: return heightAnchor.constraint(greaterThanOrEqualToConstant: size)
}
}()
constraint.isActive = true
return constraint
}
2019-09-20 08:47:27 +02:00
}