// // UIView+Autolayout.swift // InputBarAccessoryView // // Copyright © 2017-2019 Nathan Tannar. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // // Created by Nathan Tannar on 2/12/17. // import UIKit internal extension UIView { func fillSuperview() { guard let superview = self.superview else { return } translatesAutoresizingMaskIntoConstraints = false leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true topAnchor.constraint(equalTo: superview.topAnchor).isActive = true bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true } @discardableResult func addConstraints(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { if self.superview == nil { return [] } translatesAutoresizingMaskIntoConstraints = false var constraints = [NSLayoutConstraint]() if let top = top { let constraint = topAnchor.constraint(equalTo: top, constant: topConstant) constraint.identifier = "top" constraints.append(constraint) } if let left = left { let constraint = leftAnchor.constraint(equalTo: left, constant: leftConstant) constraint.identifier = "left" constraints.append(constraint) } if let bottom = bottom { let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant) constraint.identifier = "bottom" constraints.append(constraint) } if let right = right { let constraint = rightAnchor.constraint(equalTo: right, constant: -rightConstant) constraint.identifier = "right" constraints.append(constraint) } if widthConstant > 0 { let constraint = widthAnchor.constraint(equalToConstant: widthConstant) constraint.identifier = "width" constraints.append(constraint) } if heightConstant > 0 { let constraint = heightAnchor.constraint(equalToConstant: heightConstant) constraint.identifier = "height" constraints.append(constraint) } constraints.forEach { $0.isActive = true } return constraints } func removeAllConstraints() { constraints.forEach { removeConstraint($0) } } }