Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Objective-C
Posted by: David Nedrow
Added: May 17, 2018 2:20 PM
Views: 14
Tags: swift
  1. // From https://stackoverflow.com/questions/24418884/remove-all-constraints-affecting-a-uiview
  2.  
  3. extension UIView {
  4. /**
  5.  * Deactivates immediate constraints that target this view (self + superview)
  6.  */
  7.     func deactivateImmediateConstraints() {
  8.         NSLayoutConstraint.deactivate(self.immediateConstraints)
  9.     }
  10.  
  11. /**
  12.  * Deactivates all constrains that target this view
  13.  */
  14.     func deactiveAllConstraints() {
  15.         NSLayoutConstraint.deactivate(self.allConstraints)
  16.     }
  17.  
  18. /**
  19.  * Gets self.constraints + superview?.constraints for this particular view
  20.  */
  21.     var immediateConstraints: [NSLayoutConstraint] {
  22.         let constraints = self.superview?.constraints.filter {
  23.             $0.firstItem as? UIView === self || $0.secondItem as? UIView === self
  24.         } ?? []
  25.         return self.constraints + constraints
  26.     }
  27. /**
  28.  * Crawls up superview hierarchy and gets all constraints that affect this view
  29.  */
  30.     var allConstraints: [NSLayoutConstraint] {
  31.         var view: UIView? = self
  32.         var constraints: [NSLayoutConstraint] = []
  33.         while let currentView = view {
  34.             constraints += currentView.constraints.filter {
  35.                 return $0.firstItem as? UIView === self || $0.secondItem as? UIView === self
  36.             }
  37.             view = view?.superview
  38.         }
  39.         return constraints
  40.     }
  41. }
  42.