Advertisement
Don_Mag

Raildex example

Aug 30th, 2021
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.69 KB | None | 0 0
  1. enum Constraint : String {
  2.    
  3.     case top = "topAnchor"
  4.     case bottom = "bottomAnchor"
  5.     case right = "rightAnchor"
  6.     case left = "leftAnchor"
  7.     case centerX = "centerXAnchor"
  8.     case centerY = "centerYAnchor"
  9. }
  10. extension UIView {
  11.    
  12.     func constraintLeft(toLeft of: UIView, margin: CGFloat = 0) {
  13.         self.deleteConstraints(.left)
  14.         print(self.constraints) // prints []
  15.         let left = leftAnchor.constraint(equalTo: of.leftAnchor, constant: margin)
  16.         left.identifier = Constraint.left.rawValue
  17.         NSLayoutConstraint.activate([left])
  18.         setNeedsUpdateConstraints()
  19.         print(self.constraints) // prints []
  20.     }
  21.     /* Other functions left out */
  22.    
  23.     func constraintLeft(toRight of: UIView, margin: CGFloat = 0) {
  24.         self.deleteConstraints(.left)
  25.         print(self.constraints) // prints []
  26.         let left = leftAnchor.constraint(equalTo: of.rightAnchor, constant: margin)
  27.         left.identifier = Constraint.left.rawValue
  28.         NSLayoutConstraint.activate([left])
  29.         setNeedsUpdateConstraints()
  30.         print(self.constraints) // prints []
  31.     }
  32.     func constraintCenterVertical(to of: UIView) -> Void {
  33.         self.deleteConstraints(.centerY)
  34.         print(self.constraints) // prints []
  35.         let cy = centerYAnchor.constraint(equalTo: of.centerYAnchor)
  36.         cy.identifier = Constraint.centerY.rawValue
  37.         NSLayoutConstraint.activate([cy])
  38.         setNeedsUpdateConstraints()
  39.         print(self.constraints) // prints []
  40.     }
  41.    
  42.     func deleteConstraints(_ constraintsToRemove: Constraint...) {
  43.         self.removeConstraints(self.constraints.filter({ c in
  44.             guard c.identifier != nil else {
  45.                 return false
  46.             }
  47.             return constraintsToRemove.contains { constraint in
  48.                 constraint.rawValue.elementsEqual(c.identifier!)
  49.             }
  50.         }))
  51.     }
  52.    
  53. }
  54.  
  55. class CustomConstraintsViewController: UIViewController {
  56.    
  57.     override func viewDidLoad() {
  58.         super.viewDidLoad()
  59.    
  60.         let btn = createButton(icon: "cc", label: "Test")
  61.        
  62.         view.addSubview(btn)
  63.        
  64.         let g = view.safeAreaLayoutGuide
  65.         NSLayoutConstraint.activate([
  66.            
  67.             btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 80.0),
  68.             btn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 80.0),
  69.             btn.widthAnchor.constraint(equalToConstant: 240.0),
  70.             //btn.heightAnchor.constraint(equalToConstant: 120.0),
  71.            
  72.         ])
  73.        
  74.         btn.backgroundColor = .systemYellow
  75.  
  76.         btn.constraints.forEach { c in
  77.             print("create button view:", c)
  78.         }
  79.         var i = 0
  80.         btn.subviews.forEach { v in
  81.             i += 1
  82.             v.constraints.forEach { c in
  83.                 print("subview:", i, c)
  84.             }
  85.         }
  86.  
  87.     }
  88.    
  89.     func createButton(icon: String, label: String) -> UIView {
  90.         let button = UIView() // TransparentCardView()
  91.         button.translatesAutoresizingMaskIntoConstraints = false
  92.        
  93.         let uiImageView = UIImageView(image: UIImage(named: icon))
  94.         button.addSubview(uiImageView)
  95.         uiImageView.translatesAutoresizingMaskIntoConstraints = false
  96.         uiImageView.constraintCenterVertical(to: button) // works
  97.         uiImageView.constraintLeft(toLeft: button, margin: 24.0) // this does not work
  98.         //uiImageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 24.0).isActive = true // this does
  99.        
  100.         let textView = UILabel()
  101.         textView.backgroundColor = .cyan
  102.         button.addSubview(textView)
  103.         textView.translatesAutoresizingMaskIntoConstraints = false
  104.         textView.constraintCenterVertical(to: button) // works
  105.         textView.constraintLeft(toRight: uiImageView,margin: 0) // This does not work!
  106.         //textView.leftAnchor.constraint(equalTo: uiImageView.rightAnchor,constant: 20.0).isActive = true // this does work!
  107.        
  108.         textView.heightAnchor.constraint(equalToConstant: 90.0).isActive = true
  109.         textView.widthAnchor.constraint(equalToConstant: 120.0).isActive = true
  110.        
  111.         button.heightAnchor.constraint(equalToConstant: 160.0).isActive = true
  112.         return button
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement