Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import UIKit
  2.  
  3. // MARK: - UIView Anchors for Auto Layout
  4. extension UIView {
  5.  
  6. func fillSuperview() {
  7. anchor(top: superview?.topAnchor, leading: superview?.leadingAnchor, bottom: superview?.bottomAnchor, trailing: superview?.trailingAnchor)
  8. }
  9.  
  10. func anchorSize(to view: UIView) {
  11. widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
  12. heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
  13. }
  14.  
  15. func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
  16. translatesAutoresizingMaskIntoConstraints = false
  17.  
  18. if let top = top {
  19. topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
  20. }
  21. if let leading = leading {
  22. leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
  23. }
  24. if let bottom = bottom {
  25. bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom).isActive = true
  26. }
  27. if let trailing = trailing {
  28. trailingAnchor.constraint(equalTo: trailing, constant: -padding.right).isActive = true
  29. }
  30.  
  31. if size.width != 0 {
  32. widthAnchor.constraint(equalToConstant: size.width).isActive = true
  33. }
  34. if size.height != 0 {
  35. heightAnchor.constraint(equalToConstant: size.height).isActive = true
  36. }
  37.  
  38. }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement