1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public extension UIView { public func addOverlay(_ subView: UIView) { addConstraints(subView, l: 0, r: 0, t: 0, b: 0) }
public func addConstraints(_ subView: UIView, l:CGFloat? = nil, r: CGFloat? = nil, t: CGFloat? = nil, b:CGFloat? = nil, h: CGFloat? = nil, w: CGFloat? = nil, cx: CGFloat? = nil, cy: CGFloat? = nil) { addSubview(subView) subView.translatesAutoresizingMaskIntoConstraints = false
if let l = l { subView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: l).isActive = true } if let r = r { subView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: r).isActive = true } if let t = t { subView.topAnchor.constraint(equalTo: topAnchor, constant: t).isActive = true } if let b = b { subView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: b).isActive = true } if let h = h { subView.heightAnchor.constraint(equalToConstant: h).isActive = true } if let w = w { subView.widthAnchor.constraint(equalToConstant: w).isActive = true } if let cx = cx { subView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: cx).isActive = true } if let cy = cy { subView.centerYAnchor.constraint(equalTo: centerYAnchor, constant: cy).isActive = true }
} }
|