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.
		
		
		
		
		
			
		
			
	
	
		
			84 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			84 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Swift
		
	
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 | public extension UIView { | ||
| 
											6 years ago
										 |      | ||
| 
											6 years ago
										 |     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 } | ||
| 
											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
										 |     public 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
										 |     public 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
										 |     public 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
										 |     public 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
										 |     public 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
										 |     public func center(in view: UIView) { | ||
| 
											6 years ago
										 |         center(.horizontal, in: view) | ||
|  |         center(.vertical, in: view) | ||
|  |     } | ||
|  |      | ||
| 
											6 years ago
										 |     @discardableResult | ||
| 
											6 years ago
										 |     public 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
										 |     } | ||
|  | } |