在扩展中出现错误“视图层次结构没有为约束做好准备”

     2023-02-23     141

关键词:

【中文标题】在扩展中出现错误“视图层次结构没有为约束做好准备”【英文标题】:Getting the error in an extension 'The view hierarchy is not prepared for the constraint' 【发布时间】:2019-10-04 13:31:57 【问题描述】:

我在 thumbnailImageView 之上添加 thumbnailMaskView 并且它有效!

func setupViews() 

    addSubview(thumbnailImageView)
    addSubview(thumbnailMaskView)

    addConstraintsWithFormat(format: "H:|[v0]|", views: thumbnailImageView)
    addConstraintsWithFormat(format: "V:|-16-[v0]-16-|", views: thumbnailImageView)

    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .leading, relatedBy: .equal, toItem: thumbnailImageView, attribute: .leading, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .trailing, relatedBy: .equal, toItem: thumbnailImageView, attribute: .trailing, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .top, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .bottom, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 0)])

我正在尝试使用以下代码将该代码提取到 UIView Extension

extension UIView 

    func addOnTop(_ topView: UIView) 
        addConstraints([NSLayoutConstraint(item: topView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)])
    

使用extension 函数thumbnailImageView.addOnTop(thumbnailMaskView) 时出现错误:

2019-10-04 14:19:13.149081+0100 有钱人[1589:18861] [LayoutConstraints] 视图层次结构没有为 约束: (inactive)> 当添加到视图时,约束的项目必须是 该视图(或视图本身)的后代。如果 在视图层次结构之前需要解决约束 组装。打破 -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] 进行调试。 2019-10-04 14:19:13.152718+0100 富人[1589:18861] [LayoutConstraints] 查看 没有为约束做好准备的层次结构。约束: 容器层次结构: > 查看 在容器层次结构中找不到:> 该视图的 superview: > 2019-10-04 14:19:13.160046+0100 丰富 People[1589:18861] *** 由于未捕获的异常而终止应用程序 'NSGenericException',原因:'无法在视图上安装约束。 约束是否引用了子树外部的某些内容 风景?那是违法的。 约束: (活动)> 视图:>'

【问题讨论】:

你如何使用addOnTop ?? 【参考方案1】:

要向视图添加约束,那么这些约束应该用于子/后代视图,因此最好使用activate

NSLayoutConstraint.activate([  
    topView.leadingAnchor.constraint(equalTo: self.leadingAnchor), 
    topView.trailingAnchor.constraint(equalTo: self.trailingAnchor), 
    topView.topAnchor.constraint(equalTo: self.topAnchor), 
    topView.bottomAnchor.constraint(equalTo: self.bottomAnchor) 
])

它会将约束添加到正确的视图中

【讨论】:

我已经将它们添加到 addSubview(thumbnailImageView) addSubview(thumbnailMaskView) 中的视图中,这些行将占据所有视图 是的,但是 thumbnailImageView.addOnTop(thumbnailMaskView) 将两个视图之间的约束添加到 thumbnailImageView 这是不正确的,因为它们应该添加到 thumbnailImageView 的父视图中尝试 thumbnailImageView.superView!.addOnTop 并且它应该可以工作

“视图层次结构没有为约束做好准备”错误 Swift 3

】“视图层次结构没有为约束做好准备”错误Swift3【英文标题】:"Theviewhierarchyisnotpreparedfortheconstraint"errorSwift3【发布时间】:2017-04-1611:38:36【问题描述】:我正在尝试添加一个按钮并以编程方式设置约束,但我不断收到... 查看详情

视图层次结构没有为约束做好准备

...【发布时间】:2015-07-1309:00:03【问题描述】:我得到一个错误:当我运行下面的代码时,我的每个约束都位于UIView类中:funcaddIcon(iconImage:UIImage)//CreatingimageViewvaricon=UIImageView()icon.image=ic 查看详情

Swift,约束,视图层次结构没有为约束做好准备

】Swift,约束,视图层次结构没有为约束做好准备【英文标题】:Swift,Constraint,Theviewhierarchyisnotpreparedfortheconstraint【发布时间】:2014-12-1402:10:27【问题描述】:我正在尝试在导航控制器中添加图像并将其居中。所以我确实有一个im... 查看详情

视图层次结构没有为约束做好准备:无法找到问题

】视图层次结构没有为约束做好准备:无法找到问题【英文标题】:Viewhierarchyisnotpreparedfortheconstraint:UnabletoFindtheIssue【发布时间】:2016-10-1820:32:02【问题描述】:以下是正在使用的约束。【参考方案1】:在视图属于同一视图层... 查看详情

uiscrollview的自动布局,视图层次结构没有为约束做好准备

】uiscrollview的自动布局,视图层次结构没有为约束做好准备【英文标题】:uiscrollview\'sautolayout,Theviewhierarchyisnotpreparedfortheconstraint【发布时间】:2015-04-1004:30:35【问题描述】:我想使用自动布局将滚动图像视图添加到滚动视图。... 查看详情

iOS:以编程方式更改从 Interface Builder 设置的约束

...友们,美好的一天!我未能尝试通过代码为在IB中设置的视图设置动画。应用程序崩溃,原因如下:视图层次结构没有为约束做好准备...我在这里看到了一些类似的问题,原因总是以编 查看详情

st_asmvt 在使用 postgis 扩展的 Greenplum 中出现错误

】st_asmvt在使用postgis扩展的Greenplum中出现错误【英文标题】:st_asmvtgettingerrorinGreenplumwithpostgisextension【发布时间】:2021-12-0911:03:55【问题描述】:我已经成功安装了greenplum和postgis扩展。当我使用PostGIS函数st_asmvtgeom时,出现以下... 查看详情

iOS 8 键盘扩展:出现呼叫栏时出现约束错误?

】iOS8键盘扩展:出现呼叫栏时出现约束错误?【英文标题】:iOS8KeyboardExtension:ConstraintErrorWhenCallBarAppears?【发布时间】:2014-11-1021:24:31【问题描述】:我注意到,当我在打电话时,绿色的通话栏出现在屏幕顶部,我的自定义键盘... 查看详情

在 codeigniter 中出现错误

...加载一个名为“render”的库。该库的完整代码是:类渲染扩展CI_Controller公共函数模板($template,$view,$extra_css,$extra_js)$数据=数组();如果(isset($view))$d 查看详情

出现错误时如何在颤动中更改背景颜色文本框?

...景颜色?我想像这样构建文本字段这是我的代码FormData类扩展StatefulWidget@覆盖FormDataStatecreateSt 查看详情

尝试在 C 中使用 realloc 扩展数组时出现分段错误错误 [关闭]

】尝试在C中使用realloc扩展数组时出现分段错误错误[关闭]【英文标题】:SegmentationfaulterrorwhiletryingtoexpandarraywithreallocinC[closed]【发布时间】:2016-05-2313:22:43【问题描述】:我的代码运行良好,最多3次迭代就可以得到正确的结果... 查看详情

在 C# 中使用扩展方法时出错

】在C#中使用扩展方法时出错【英文标题】:ErrorwhenusingextensionmethodsinC#【发布时间】:2010-09-1708:46:32【问题描述】:我遇到了一个问题,让我认为3.0框架中存在错误。当我尝试使用扩展方法时,出现以下错误:Missingcompilerrequiredm... 查看详情

在 Swift 中出现错误对成员“下标”的模糊引用

...布时间】:2016-11-0210:10:55【问题描述】:Dictionary的必需扩展,以获取文本键值(如果存在)。实现如下代码,编译成功:extensionDictionarywhereKey:ExpressibleByStringLiteral,Va 查看详情

在创建新 AD 林和域期间重新启动后,带有 DSC 扩展的 ARM 模板失败并出现安全错误

】在创建新AD林和域期间重新启动后,带有DSC扩展的ARM模板失败并出现安全错误【英文标题】:ARMtemplatewithDSCextensionfailswithsecurityerrorafterrebootduringcreatenewADforestanddomain【发布时间】:2020-09-1102:43:12【问题描述】:几个月来,我一... 查看详情

客户登录时,清漆在 HTML 标头中出现 403 禁止错误

...Varnish,并且我正在使用为Magento管理Varnish的MagentoTurpentine扩展,当登录的用户访问一个没有被Var 查看详情

JSLint 中出现意外的“=”错误

...发布时间】:2012-10-2115:37:31【问题描述】:我在一个zepto扩展js文件上使用了JSLint它抛出了错误:Unexpected\'=\'.line33character37第33行:return(document.cookie=[encodeURIComponent(key),\'=\',options.raw 查看详情

在“PARALLEL”处或附近创建 postgis 扩展语法错误

】在“PARALLEL”处或附近创建postgis扩展语法错误【英文标题】:createpostgisextensionsyntaxerroratornear"PARALLEL"【发布时间】:2017-02-2100:23:53【问题描述】:当我尝试在windows上的postgresql中启用postgis扩展时,我收到以下错误:“PA... 查看详情

错误:无法在 Mavericks 上构建 gem 原生扩展

】错误:无法在Mavericks上构建gem原生扩展【英文标题】:ERROR:FailedtobuildgemnativeextensiononMavericks【发布时间】:2013-11-0308:14:26【问题描述】:我正在尝试在OSX10.9上的Rails项目中运行bundle。到达pggem时失败并出现此错误:Gem::Installer::... 查看详情