ios - 将框架/边界/约束从一个 View 复制到另一个 View

标签 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 View 具有相同尺寸的模糊 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 及其 subview 有关。在 self.blurView 上设置这些约束并不会神奇地将它们更改为引用 self.blurView 及其 subview 。您正在向与其或其 subview 无关的 View 添加约束。

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

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

关于ios - 将框架/边界/约束从一个 View 复制到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28527272/

相关文章:

ios - 关于电话通讯录iOS排序的信息

ios - 解析 eBay API 时使用未声明的标识符 'cell'

ios - 如何清除图像缓存或 AFNetworking 中的任何缓存?

iphone - 应用程序仅在iPhone设备上崩溃而不在模拟器中崩溃

ios - libc++abi.dylib : terminating with uncaught exception of type NSException without the regular error message

ios - 当我向我的 friend ios xcode 发送邀请时,在 facebook 中看不到通知

ios - 如何使 SKSpriteNode 不掉落(Swift)

ios - 应用程序关闭后使用 MagicalRecord 截断所有内容而不保存

ios - 在 UIStackView 控件中调整 UIView 高度

ios - 仅限生产版本的 EXC_BREAKPOINT (SIGTRAP)