Don_Mag icon

Raildex example

Don_Mag | PRO | 08/30/21 01:13:42 PM UTC | 0 ⭐ | 8438 👁️ | Never ⏰ | []
Swift |

3.69 KB

|

None

|

0 👍

/

0 👎

enum Constraint : String {
    
    case top = "topAnchor"
    case bottom = "bottomAnchor"
    case right = "rightAnchor"
    case left = "leftAnchor"
    case centerX = "centerXAnchor"
    case centerY = "centerYAnchor"
}
extension UIView {
    
    func constraintLeft(toLeft of: UIView, margin: CGFloat = 0) {
        self.deleteConstraints(.left)
        print(self.constraints) // prints []
        let left = leftAnchor.constraint(equalTo: of.leftAnchor, constant: margin)
        left.identifier = Constraint.left.rawValue
        NSLayoutConstraint.activate([left])
        setNeedsUpdateConstraints()
        print(self.constraints) // prints []
    }
    /* Other functions left out */
    
    func constraintLeft(toRight of: UIView, margin: CGFloat = 0) {
        self.deleteConstraints(.left)
        print(self.constraints) // prints []
        let left = leftAnchor.constraint(equalTo: of.rightAnchor, constant: margin)
        left.identifier = Constraint.left.rawValue
        NSLayoutConstraint.activate([left])
        setNeedsUpdateConstraints()
        print(self.constraints) // prints []
    }
    func constraintCenterVertical(to of: UIView) -> Void {
        self.deleteConstraints(.centerY)
        print(self.constraints) // prints []
        let cy = centerYAnchor.constraint(equalTo: of.centerYAnchor)
        cy.identifier = Constraint.centerY.rawValue
        NSLayoutConstraint.activate([cy])
        setNeedsUpdateConstraints()
        print(self.constraints) // prints []
    }
    
    func deleteConstraints(_ constraintsToRemove: Constraint...) {
        self.removeConstraints(self.constraints.filter({ c in
            guard c.identifier != nil else {
                return false
            }
            return constraintsToRemove.contains { constraint in
                constraint.rawValue.elementsEqual(c.identifier!)
            }
        }))
    }
    
}
 
class CustomConstraintsViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let btn = createButton(icon: "cc", label: "Test")
        
        view.addSubview(btn)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            
            btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 80.0),
            btn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 80.0),
            btn.widthAnchor.constraint(equalToConstant: 240.0),
            //btn.heightAnchor.constraint(equalToConstant: 120.0),
            
        ])
        
        btn.backgroundColor = .systemYellow
 
        btn.constraints.forEach { c in
            print("create button view:", c)
        }
        var i = 0
        btn.subviews.forEach { v in
            i += 1
            v.constraints.forEach { c in
                print("subview:", i, c)
            }
        }
 
    }
    
    func createButton(icon: String, label: String) -> UIView {
        let button = UIView() // TransparentCardView()
        button.translatesAutoresizingMaskIntoConstraints = false
        
        let uiImageView = UIImageView(image: UIImage(named: icon))
        button.addSubview(uiImageView)
        uiImageView.translatesAutoresizingMaskIntoConstraints = false
        uiImageView.constraintCenterVertical(to: button) // works
        uiImageView.constraintLeft(toLeft: button, margin: 24.0) // this does not work
        //uiImageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 24.0).isActive = true // this does
        
        let textView = UILabel()
        textView.backgroundColor = .cyan
        button.addSubview(textView)
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.constraintCenterVertical(to: button) // works
        textView.constraintLeft(toRight: uiImageView,margin: 0) // This does not work!
        //textView.leftAnchor.constraint(equalTo: uiImageView.rightAnchor,constant: 20.0).isActive = true // this does work!
        
        textView.heightAnchor.constraint(equalToConstant: 90.0).isActive = true
        textView.widthAnchor.constraint(equalToConstant: 120.0).isActive = true
        
        button.heightAnchor.constraint(equalToConstant: 160.0).isActive = true
        return button
    }
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎