将框架/边界/约束从一个视图复制到另一个视图 [英] Copying frame/bounds/constraints from one view to another

查看:25
本文介绍了将框架/边界/约束从一个视图复制到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

self.blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight)) as UIVisualEffectView
self.blurView.frame = self.filterPanel.frame
self.blurView.bounds = self.filterPanel.bounds
self.blurView.addConstraints(self.filterPanel.constraints())
self.view.insertSubview(self.blurView, belowSubview: self.filterPanel)

当试图在 viewDidLoad 中运行上面的代码时,它会返回下面的错误.为什么是这样?我只是想创建一个与 filterPanel 视图具有相同尺寸的模糊视图.

When attempting to run the code above in viewDidLoad it returns the error below. Why is this? I am simply trying to create a blur view with the same dimensions as the filterPanel view.

2015-02-15 14:44:37.929 App[568:182030] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x18eeac70 H:|-(0)-[UIView:0x1bbe0a70]   (Names: '|':UIView:0x1bbe0a00 )>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-02-15 14:44:37.944 App[568:182030] *** Assertion failure in -[UIVisualEffectView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /SourceCache/UIKit/UIKit-3318.16.25/NSLayoutConstraint_UIKitAdditions.m:560
2015-02-15 14:44:37.945 App[568:182030] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Impossible to set up layout with view hierarchy unprepared for constraint.'
*** First throw call stack:
(0x229ba49f 0x30174c8b 0x229ba375 0x2368bd1f 0x264a4295 0x25f63cd3 0x25f63b3f 0x25f63ab3 0x264a43d1 0x25f6c909 0x25f6c87d 0xffe30 0x1019dc 0x25e7b52f 0x25f25385 0x25f252ad 0x25f24843 0x25f24573 0x25f242dd 0x25f24271 0x25e78a6f 0x258a0a0d 0x2589c3e5 0x2589c26d 0x2589bc51 0x2589ba55 0x260dc885 0x260dd62d 0x260e7a39 0x260dba47 0x2914b0d1 0x22980d7d 0x22980041 0x2297e7c3 0x228cc3c1 0x228cc1d3 0x25edf9cf 0x25eda7b1 0xbd574 0xbd690 0x306f4aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

提前致谢.

推荐答案

约束 self.filterPanel.constraints() 大概都与 self.filterPanel 及其子视图有关.在 self.blurView 上设置这些约束并不会神奇地将它们更改为引用 self.blurView 及其子视图.您正在向与其或其子视图无关的视图添加约束.

The constraints self.filterPanel.constraints() all presumably relate to self.filterPanel and its subviews. Setting those constraints on self.blurView doesn't magically change them to refer to self.blurView and its subviews. You're adding constraints to a view that has nothing to do with it or its subviews.

您需要遍历所有约束并构建新的约束,这些约束在形式上是并行的,但引用 self.blurView 子层次结构中的相应视图,这不会是简单.一方面,您刚刚创建了 self.blurView,因此它可能还没有子视图.

You would need to go through all of the constraints and build new constraints that are parallel in form but refer to the corresponding view within the self.blurView sub-hierarchy, which is not going to be easy. For one thing, you've just created self.blurView so it presumably has no subviews, yet.

您可以尝试将原始视图编码为存档,然后对该存档进行解码以生成重复的子层次结构.

You might try encoding the original view to an archive and then decoding that archive to produce a duplicate sub-hierarchy.

这篇关于将框架/边界/约束从一个视图复制到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆