简体   繁体   中英

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.

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 
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM