Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension UIView {
- func addConstraintsWithFormat(_ format: String, views: UIView...) {
- var viewsDictionary = [String: UIView]()
- for (index, view) in views.enumerated() {
- let key = "v\(index)"
- view.translatesAutoresizingMaskIntoConstraints = false
- viewsDictionary[key] = view
- }
- addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
- }
- func centerHorizontallyWithOffset(_ subview: UIView, offset: CGFloat) {
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: offset))
- }
- // center subview horizontally in view
- func centerHorizontally(_ subview: UIView) {
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
- }
- // combine with centerhorizontally and simply add an optional parameter
- func centerVerticallyWithOffset(_ subview: UIView, offset: CGFloat) {
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
- }
- // center subview vertically in view
- func centerVertically(_ subview: UIView, multiplier: CGFloat = 1.0, offset: CGFloat = 0.0) {
- // from https://autolayoutconstraints.com/
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: multiplier, constant: offset))
- }
- func unCenterVertically(_ subview: UIView) {
- removeConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))
- }
- // add constraint for width to equal height
- func makeSquare(_ subview: UIView) {
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: subview, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0.0))
- }
- // make a subview a percentage from the top of the superview (this)
- func percentFromTop(_ subview: UIView, percent: CGFloat, constant: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal,
- toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
- }
- // constrain a child view to its sibling based on the distance between the top of the child to the bottom of the sibling
- func percentFromSibling(_ child: UIView, sibling: UIView, percent: CGFloat, constant: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal,
- toItem: sibling, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
- }
- func widthPercentOfParent(_ child: UIView, percent: CGFloat) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: percent, constant: 0.0))
- }
- func heightPercentOfParent(_ child: UIView, percent: CGFloat) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.height, multiplier: percent, constant: 0.0))
- }
- func widthPercentOfSibling(_ child: UIView, sibling: UIView, percent: CGFloat, offset: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.width, multiplier: percent, constant: offset))
- }
- func heightPercentOfSibling(_ child: UIView, sibling: UIView, percent: CGFloat, offset: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.height, multiplier: percent, constant: offset))
- }
- func centerChildVerticallyToSibling(_ child: UIView, sibling: UIView, offset: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
- }
- func centerChildHorizontallyToSibling(_ child: UIView, sibling: UIView, offset: CGFloat = 0) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
- }
- func centerChildToSibling(_ child: UIView, sibling: UIView) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0))
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))
- }
- func spaceVerticallyFromSibling(_ child: UIView, sibling: UIView, constant: CGFloat = 20, percent: CGFloat = 1) {
- addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
- }
- func fillAndCenter(_ child: UIView) {
- addConstraintsWithFormat("V:|[v0]|", views: child)
- addConstraintsWithFormat("H:|[v0]|", views: child)
- }
- func makeRound() {
- layer.cornerRadius = frame.width / 2
- clipsToBounds = true
- //layer.masksToBounds = true
- }
- // add multiple subviews at once
- func addSubviews(_ views: UIView...) {
- for (_, view) in views.enumerated() {
- addSubview(view)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement