UIView+AutoLayout.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // UIView+Autolayout.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 2/12/17.
  26. //
  27. import UIKit
  28. internal extension UIView {
  29. func fillSuperview() {
  30. guard let superview = self.superview else {
  31. return
  32. }
  33. translatesAutoresizingMaskIntoConstraints = false
  34. leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
  35. rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
  36. topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
  37. bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
  38. }
  39. @discardableResult
  40. 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] {
  41. if self.superview == nil {
  42. return []
  43. }
  44. translatesAutoresizingMaskIntoConstraints = false
  45. var constraints = [NSLayoutConstraint]()
  46. if let top = top {
  47. let constraint = topAnchor.constraint(equalTo: top, constant: topConstant)
  48. constraint.identifier = "top"
  49. constraints.append(constraint)
  50. }
  51. if let left = left {
  52. let constraint = leftAnchor.constraint(equalTo: left, constant: leftConstant)
  53. constraint.identifier = "left"
  54. constraints.append(constraint)
  55. }
  56. if let bottom = bottom {
  57. let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant)
  58. constraint.identifier = "bottom"
  59. constraints.append(constraint)
  60. }
  61. if let right = right {
  62. let constraint = rightAnchor.constraint(equalTo: right, constant: -rightConstant)
  63. constraint.identifier = "right"
  64. constraints.append(constraint)
  65. }
  66. if widthConstant > 0 {
  67. let constraint = widthAnchor.constraint(equalToConstant: widthConstant)
  68. constraint.identifier = "width"
  69. constraints.append(constraint)
  70. }
  71. if heightConstant > 0 {
  72. let constraint = heightAnchor.constraint(equalToConstant: heightConstant)
  73. constraint.identifier = "height"
  74. constraints.append(constraint)
  75. }
  76. constraints.forEach { $0.isActive = true }
  77. return constraints
  78. }
  79. func removeAllConstraints() {
  80. constraints.forEach { removeConstraint($0) }
  81. }
  82. }