NSConstraintLayoutSet.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. internal class NSLayoutConstraintSet {
  22. internal var top: NSLayoutConstraint?
  23. internal var bottom: NSLayoutConstraint?
  24. internal var left: NSLayoutConstraint?
  25. internal var right: NSLayoutConstraint?
  26. internal var centerX: NSLayoutConstraint?
  27. internal var centerY: NSLayoutConstraint?
  28. internal var width: NSLayoutConstraint?
  29. internal var height: NSLayoutConstraint?
  30. internal init(top: NSLayoutConstraint? = nil, bottom: NSLayoutConstraint? = nil,
  31. left: NSLayoutConstraint? = nil, right: NSLayoutConstraint? = nil,
  32. centerX: NSLayoutConstraint? = nil, centerY: NSLayoutConstraint? = nil,
  33. width: NSLayoutConstraint? = nil, height: NSLayoutConstraint? = nil) {
  34. self.top = top
  35. self.bottom = bottom
  36. self.left = left
  37. self.right = right
  38. self.centerX = centerX
  39. self.centerY = centerY
  40. self.width = width
  41. self.height = height
  42. }
  43. /// All of the currently configured constraints
  44. private var availableConstraints: [NSLayoutConstraint] {
  45. let constraints = [top, bottom, left, right, centerX, centerY, width, height]
  46. var available: [NSLayoutConstraint] = []
  47. for constraint in constraints {
  48. if let value = constraint {
  49. available.append(value)
  50. }
  51. }
  52. return available
  53. }
  54. /// Activates all of the non-nil constraints
  55. ///
  56. /// - Returns: Self
  57. @discardableResult
  58. internal func activate() -> Self {
  59. NSLayoutConstraint.activate(availableConstraints)
  60. return self
  61. }
  62. /// Deactivates all of the non-nil constraints
  63. ///
  64. /// - Returns: Self
  65. @discardableResult
  66. internal func deactivate() -> Self {
  67. NSLayoutConstraint.deactivate(availableConstraints)
  68. return self
  69. }
  70. }