Advertisement
SlothNGU

123

Oct 8th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import UIKit
  2. extension UIView {
  3.  
  4. func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, enableInsets: Bool) {
  5. var topInset = CGFloat(0)
  6. var bottomInset = CGFloat(0)
  7.  
  8. if #available(iOS 11, *), enableInsets {
  9. let insets = self.safeAreaInsets
  10. topInset = insets.top
  11. bottomInset = insets.bottom
  12.  
  13. print(“Top: \(topInset)”)
  14. print(“bottom: \(bottomInset)”)
  15. }
  16.  
  17. translatesAutoresizingMaskIntoConstraints = false
  18.  
  19. if let top = top {
  20. self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true
  21. }
  22. if let left = left {
  23. self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
  24. }
  25. if let right = right {
  26. rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
  27. }
  28. if let bottom = bottom {
  29. bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true
  30. }
  31. if height != 0 {
  32. heightAnchor.constraint(equalToConstant: height).isActive = true
  33. }
  34. if width != 0 {
  35. widthAnchor.constraint(equalToConstant: width).isActive = true
  36. }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement