Tom
Tom

Reputation: 247

Export or Print Auto Layout Constraints from Storyboard iOS

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.

Upvotes: 2

Views: 2482

Answers (3)

William Hu
William Hu

Reputation: 16149

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.

Upvotes: 0

Kasper Munck
Kasper Munck

Reputation: 4191

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 
}

Upvotes: 3

Stefan
Stefan

Reputation: 5451

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

Upvotes: 0

Related Questions