Last active
October 9, 2021 06:11
-
-
Save Achyut-Sagar/66696b551d9a78db5a0a4ae6efe63b96 to your computer and use it in GitHub Desktop.
Extension of UIview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UIView{ | |
| //MARK:- Frame Properties | |
| var originPoint : CGPoint{ | |
| return self.frame.origin | |
| } | |
| var originX : CGFloat{ | |
| return self.frame.origin.x | |
| } | |
| var originY : CGFloat{ | |
| return self.frame.origin.y | |
| } | |
| var frameSize : CGSize{ | |
| return self.frame.size | |
| } | |
| var frameWidth : CGFloat{ | |
| return self.frame.size.width | |
| } | |
| var frameHeight : CGFloat{ | |
| return self.frame.size.height | |
| } | |
| } | |
| //MARK: - Constraint Methods | |
| extension UIView{ | |
| @objc func constraintsForAnchoringTo(boundsOf view: UIView, inset: UIEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0)) -> [NSLayoutConstraint] { | |
| return [ | |
| topAnchor.constraint(equalTo: view.topAnchor, constant: inset.top), | |
| leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: inset.left), | |
| bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -1 * inset.bottom), | |
| trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -1 * inset.right) | |
| ] | |
| } | |
| /// Returns Applied constraint by identifier | |
| /// - Parameter identifier: constraint identifier (normally set up manually in storyboard in size inspector of constraint) | |
| /// - Returns: Constraint | |
| @objc func getConstraint(by identifier: String) -> NSLayoutConstraint?{ | |
| let constraint = self.constraints.filter({$0.identifier == identifier}).first | |
| return constraint | |
| } | |
| /// Sets up provided constraint to this view | |
| /// - Parameter constraints: array of constraints | |
| @objc func layout(using constraints:[NSLayoutConstraint]){ | |
| self.translatesAutoresizingMaskIntoConstraints=false | |
| NSLayoutConstraint.activate(constraints) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment