session-ios/SignalMessaging/Loki/UIView+Constraints.swift

84 lines
3.3 KiB
Swift
Raw Normal View History

2019-09-20 08:47:27 +02:00
2020-01-20 03:20:27 +01:00
public extension UIView {
2019-09-20 08:47:27 +02:00
2020-01-20 03:20:27 +01:00
public enum HorizontalEdge { case left, leading, right, trailing }
public enum VerticalEdge { case top, bottom }
public enum Direction { case horizontal, vertical }
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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-01-20 03:20:27 +01:00
public 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
}
}