简体   繁体   English

从Storyboard iOS导出或打印自动布局约束

[英]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. 我想用代码重新创建在Storyboard中所做的事情。

All instances of UIView has a property named constraints . UIView所有实例都有一个名为constraints的属性。 That is an NSArray of all the NSLayoutConstraint 's associated with that particular view. 那是与该特定视图关联的所有NSLayoutConstraintNSArray 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)? 您是否尝试在外部编辑器(不是xCode)中打开情节提要文件? You can read it, it's just xml. 您可以阅读它,它只是xml。

If you need print the view you should find the constraints in the super view. 如果需要打印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. 接受的答案应仅打印view本身的heightwidth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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