将框架/边界/约束从一个视图复制到另一个视图

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中运行上面的代码时,它返回下面的错误。 为什么是这样? 我只是试图创build一个与filterPanel视图相同尺寸的filterPanel视图。

 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及其子视图。 您将限制添加到与其子视图无关的视图。

你将需要通过所有的约束,并build立新的约束,在forms上是平行的,但在self.blurView子层次结构中引用相应的视图,这不是一件容易的事情。 首先,你刚刚创build了self.blurView所以它可能没有子视图。

您可以尝试将原始视图编码到档案,然后解码该档案以产生重复的子层次。