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

时间:2015-02-15 14:49:57

标签: ios objective-c xcode swift

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视图相同的尺寸创建模糊视图。

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) 

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

您需要遍历所有约束并构建表单中并行的新约束,但需要引用self.blurView子层次结构中的相应视图,这并不容易。首先,您刚刚创建了self.blurView,因此它可能没有子视图。

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