2

Is it possible to print out the constraints set up in auto layout that you create in a Storyboard? I would like to re-create what I have done in the Storyboard in code.

Tom
  • 247
  • 1
  • 17

3 Answers3

3

All instances of UIView has a property named constraints. That is an NSArray of all the NSLayoutConstraint's associated with that particular view. You could traverse a view hierarchy recursively from the root view and then print out the desired layout constraint properties.

// for each view in hierarchy
for (NSLayoutConstraint *constraint in view.constraints) {
    // print what you need here 
}
Community
  • 1
  • 1
Kasper Munck
  • 4,173
  • 2
  • 27
  • 50
0

Have you tried to open the storyboard file in an external editor (not xCode)? You can read it, it's just xml.

Stefan
  • 5,203
  • 8
  • 27
  • 51
0

If you need print the view you should find the constraints in the super view. For example:

         view.superview?.constraints.forEach { (constraints: NSLayoutConstraint) in
           // If you want to find some specific constrainst
            if constraints.firstItem as! NSObject == view && (constraints.firstAttribute == .centerX) {
                // Remove it.
                constraints.isActive = false
            }
        }

The accepted answer should only print height and width of the view itself.

William Hu
  • 15,423
  • 11
  • 100
  • 121