【问题标题】:Copying frame/bounds/constraints from one view to another将框架/边界/约束从一个视图复制到另一个视图
【发布时间】:2015-02-15 14:49:56
【问题描述】:
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) 

提前致谢。

【问题讨论】:

    标签: ios objective-c xcode swift


    【解决方案1】:

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

    您将需要遍历所有约束并构建形式上平行但引用self.blurView 子层次结构中的相应视图的新约束,这并不容易。一方面,您刚刚创建了self.blurView,所以它可能还没有子视图。

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

    【讨论】:

    • 谢谢肯。由于约束不多,我将手动添加。
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多