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.
Asked
Active
Viewed 2,462 times
3 Answers
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