mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			99 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			99 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | import UIKit | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 | public protocol ConstraintUtilitiesEdge { } | ||
|  | 
 | ||
| 
											6 years ago
										 | public extension UIView { | ||
| 
											6 years ago
										 |      | ||
| 
											6 years ago
										 |     enum HorizontalEdge : ConstraintUtilitiesEdge { case left, leading, right, trailing } | ||
|  |     enum VerticalEdge : ConstraintUtilitiesEdge { case top, bottom } | ||
|  |     enum Direction { case horizontal, vertical } | ||
|  |     enum Dimension { case width, height } | ||
| 
											6 years ago
										 |      | ||
|  |     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 | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
| 
											6 years ago
										 |     @discardableResult | ||
| 
											6 years ago
										 |     func pin(_ constraineeEdge: HorizontalEdge, to constrainerEdge: HorizontalEdge, of view: UIView, withInset inset: CGFloat = 0) -> NSLayoutConstraint { | ||
| 
											6 years ago
										 |         translatesAutoresizingMaskIntoConstraints = false | ||
| 
											6 years ago
										 |         let constraint = anchor(from: constraineeEdge).constraint(equalTo: view.anchor(from: constrainerEdge), constant: inset) | ||
|  |         constraint.isActive = true | ||
|  |         return constraint | ||
| 
											6 years ago
										 |     } | ||
|  |      | ||
| 
											6 years ago
										 |     @discardableResult | ||
| 
											6 years ago
										 |     func pin(_ constraineeEdge: VerticalEdge, to constrainerEdge: VerticalEdge, of view: UIView, withInset inset: CGFloat = 0) -> NSLayoutConstraint { | ||
| 
											6 years ago
										 |         translatesAutoresizingMaskIntoConstraints = false | ||
| 
											6 years ago
										 |         let constraint = anchor(from: constraineeEdge).constraint(equalTo: view.anchor(from: constrainerEdge), constant: inset) | ||
|  |         constraint.isActive = true | ||
|  |         return constraint | ||
| 
											6 years ago
										 |     } | ||
|  |      | ||
| 
											6 years ago
										 |     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) { | ||
| 
											6 years ago
										 |         [ HorizontalEdge.leading, HorizontalEdge.trailing ].forEach { pin($0, to: $0, of: view) } | ||
|  |         [ VerticalEdge.top, VerticalEdge.bottom ].forEach { pin($0, to: $0, of: view) } | ||
|  |     } | ||
|  |      | ||
| 
											6 years ago
										 |     func pin(to view: UIView, withInset inset: CGFloat) { | ||
| 
											6 years ago
										 |         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) | ||
|  |     } | ||
|  |      | ||
| 
											6 years ago
										 |     @discardableResult | ||
| 
											6 years ago
										 |     func center(_ direction: Direction, in view: UIView) -> NSLayoutConstraint { | ||
| 
											6 years ago
										 |         translatesAutoresizingMaskIntoConstraints = false | ||
| 
											6 years ago
										 |         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 | ||
| 
											6 years ago
										 |     } | ||
|  |      | ||
| 
											6 years ago
										 |     func center(in view: UIView) { | ||
| 
											6 years ago
										 |         center(.horizontal, in: view) | ||
|  |         center(.vertical, in: view) | ||
|  |     } | ||
|  |      | ||
| 
											6 years ago
										 |     @discardableResult | ||
| 
											6 years ago
										 |     func set(_ dimension: Dimension, to size: CGFloat) -> NSLayoutConstraint { | ||
| 
											6 years ago
										 |         translatesAutoresizingMaskIntoConstraints = false | ||
| 
											6 years ago
										 |         let constraint: NSLayoutConstraint = { | ||
|  |             switch dimension { | ||
|  |             case .width: return widthAnchor.constraint(equalToConstant: size) | ||
|  |             case .height: return heightAnchor.constraint(equalToConstant: size) | ||
|  |             } | ||
|  |         }() | ||
|  |         constraint.isActive = true | ||
|  |         return constraint | ||
| 
											6 years ago
										 |     } | ||
|  | } |