Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- public extension UIView {
- func addHeightConstraint(_ height: CGFloat) {
- addConstraint(NSLayoutConstraint(item: self,
- attribute: .height,
- relatedBy: .equal,
- toItem: nil,
- attribute: .notAnAttribute,
- multiplier: 1.0,
- constant: height))
- }
- func addHeightWidthRatio( _ ratio: CGFloat) {
- addConstraint(NSLayoutConstraint(item: self,
- attribute: .height,
- relatedBy: .equal,
- toItem: self,
- attribute: .width,
- multiplier: ratio, constant: 0))
- }
- func addWidthConstraint(_ height: CGFloat) {
- addConstraint(NSLayoutConstraint(item: self,
- attribute: .width,
- relatedBy: .equal,
- toItem: nil,
- attribute: .notAnAttribute,
- multiplier: 1.0,
- constant: height))
- }
- func centerXAxisToSuperView() {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(item: self,
- attribute: .centerX,
- relatedBy: .equal,
- toItem: superview,
- attribute: .centerX,
- multiplier: 1,
- constant: 0))
- }
- func centerYAxisToSuperView() {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(item: self,
- attribute: .centerY,
- relatedBy: .equal,
- toItem: superview,
- attribute: .centerY,
- multiplier: 1,
- constant: 0))
- }
- func centerToSuperView() {
- centerXAxisToSuperView()
- centerYAxisToSuperView()
- }
- func stickEdgesToSuperView(edges: [NSLayoutAttribute] = [.leading, .trailing, .top, .bottom], spaces: [CGFloat] = [0.0, 0.0, 0.0, 0.0]) {
- guard let superview = superview, edges.count == spaces.count else { return }
- for (index, edge) in edges.enumerated() {
- switch edge {
- case .top, .leading:
- superview.addConstraint(NSLayoutConstraint(item: self,
- attribute: edge,
- relatedBy: .equal,
- toItem: superview,
- attribute: edge,
- multiplier: 1.0,
- constant: spaces[index]))
- case .bottom, .trailing:
- superview.addConstraint(NSLayoutConstraint(item: superview,
- attribute: edge,
- relatedBy: .equal,
- toItem: self,
- attribute: edge,
- multiplier: 1.0,
- constant: spaces[index]))
- default:
- break
- }
- }
- }
- func stickToTopView(topView: UIView, _ space: CGFloat) {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(
- item: self,
- attribute: .top,
- relatedBy: .equal,
- toItem: topView,
- attribute: topView == superview ? .top : .bottom,
- multiplier: 1.0,
- constant: space))
- }
- func stickToBottomView(bottomView: UIView, _ space: CGFloat) {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(
- item: bottomView,
- attribute: bottomView == superview ? .bottom : .top,
- relatedBy: .equal,
- toItem: self,
- attribute: .bottom,
- multiplier: 1.0,
- constant: space))
- }
- func stickToLeftView(leftView: UIView, _ space: CGFloat) {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(
- item: self,
- attribute: .leading,
- relatedBy: .equal,
- toItem: leftView,
- attribute: leftView == superview ? .leading : .trailing,
- multiplier: 1.0,
- constant: space))
- }
- func stickToRightView(rightView: UIView, _ space: CGFloat) {
- guard let superview = superview else { return }
- superview.addConstraint(NSLayoutConstraint(
- item: rightView,
- attribute: rightView == superview ? .trailing : .leading,
- relatedBy: .equal,
- toItem: self,
- attribute: .trailing,
- multiplier: 1.0,
- constant: space))
- }
- }
Add Comment
Please, Sign In to add comment