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

     2023-03-15     128

关键词:

【中文标题】“视图层次结构没有为约束做好准备”错误 Swift 3【英文标题】:"The view hierarchy is not prepared for the constraint" error Swift 3 【发布时间】:2017-04-16 11:38:36 【问题描述】:

我正在尝试添加一个按钮并以编程方式设置约束,但我不断收到此错误并且无法弄清楚我的代码有什么问题。我在这里查看了其他问题,但它们对我的情况并没有太大帮助。

    btn.setTitle("mybtn", for: .normal)
    btn.setTitleColor(UIColor.blue, for: .normal)
    btn.backgroundColor = UIColor.lightGray
    view.addSubview(btn)
    btn.translatesAutoresizingMaskIntoConstraints = false

    let left = NSLayoutConstraint(item: btn, attribute: .leftMargin, relatedBy: .equal, toItem: view, attribute: .leftMargin, multiplier: 1.0, constant: 0)

    let right = NSLayoutConstraint(item: btn, attribute: .rightMargin, relatedBy: .equal, toItem: view, attribute: .rightMargin, multiplier: 1.0, constant: 0)

    let top = NSLayoutConstraint(item: btn, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0)

    btn.addConstraints([left, right, top])

【问题讨论】:

你把这段代码放在哪里? 【参考方案1】:

当adding constraints 指向一个视图时,“[约束中] 所涉及的任何视图必须要么是接收视图本身,要么是接收视图的子视图”。您将约束添加到btn,因此它不了解约束所引用的view 的含义,因为它既不是btn,也不是btn 的子视图。如果您将约束添加到view,而不是btn,则会解决该错误。

或者更好,正如 Khalid 所说,改用 activate,在这种情况下,您无需担心在视图层次结构中添加约束的位置:

let btn = UIButton(type: .system)
btn.setTitle("mybtn", for: .normal)
btn.setTitleColor(.blue, for: .normal)
btn.backgroundColor = .lightGray
view.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    btn.leftAnchor.constraint(equalTo: view.leftAnchor),
    btn.rightAnchor.constraint(equalTo: view.rightAnchor),
    btn.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
])

【讨论】:

谢谢。一直在找这个。【参考方案2】:

使用激活约束。在 iOS 9 下,您可以使用激活

 btn.setTitle("mybtn", for: .normal)
 btn.setTitleColor(UIColor.blue, for: .normal)
 btn.backgroundColor = UIColor.gray

 btn.translatesAutoresizingMaskIntoConstraints = false
 self.view.addSubview(btn)


let left = NSLayoutConstraint(item: btn, attribute: .leftMargin, relatedBy: .equal, toItem: view, attribute: .leftMargin, multiplier: 1.0, constant: 0)

let right = NSLayoutConstraint(item: btn, attribute: .rightMargin, relatedBy: .equal, toItem: view, attribute: .rightMargin, multiplier: 1.0, constant: 0)

let top = NSLayoutConstraint(item: btn, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0)

// here you have to call activate constraints everything will work     
NSLayoutConstraint.activate([left, right, top])

example project

【讨论】:

那个NSLayoutConstraint.activate 方法实际上只是一个捷径兄弟。来自 Apple 文档:在此约束管理的项目的最近共同祖先的视图上激活或停用约束调用 addConstraint(_:)removeConstraint(_:) 对不起,如果我不礼貌 这就是我如此热爱 SO 社区的原因……我们让代码来说话:-) KhalidAfridi,@Rob【参考方案3】:

尝试将leftrightconstraints 添加到view 而不是btn

【讨论】:

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

】视图层次结构没有为约束做好准备?【英文标题】:Theviewhierarchyisnotpreparedfortheconstraint?【发布时间】:2015-08-1219:51:15【问题描述】:我收到以下错误,即使我在添加按钮和应用约束之前设置了表格视图页脚。在-viewDidLoad和-view... 查看详情

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

】在扩展中出现错误“视图层次结构没有为约束做好准备”【英文标题】:Gettingtheerrorinanextension\'Theviewhierarchyisnotpreparedfortheconstraint\'【发布时间】:2019-10-0413:31:57【问题描述】:我在thumbnailImageView之上添加thumbnailMaskView并且它... 查看详情

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

】视图层次结构没有为约束做好准备【英文标题】:Theviewhierarchyisnotpreparedfortheconstraint【发布时间】:2015-07-1309:00:03【问题描述】:我得到一个错误:当我运行下面的代码时,我的每个约束都位于UIView类中:funcaddIcon(iconImage:UIIma... 查看详情

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

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

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

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

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

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

Swift 3.0 转换错误

】Swift3.0转换错误【英文标题】:Swift3.0convertingerror【发布时间】:2016-09-2414:54:59【问题描述】:我有一个Swift2.2项目。现在我正在尝试将其转换为Swift3.0但出现错误。斯威夫特2.2:importQuartzCore.QuartzCore但是这段代码出现错误。Swif... 查看详情

从 Swift 2.3 迁移到 Swift 3 错误消息

】从Swift2.3迁移到Swift3错误消息【英文标题】:MigrationfromSwift2.3toSwift3errormessage【发布时间】:2016-11-0311:15:47【问题描述】:我一直在尝试将Swift项目迁移到最新的Swift版本。fileprivatelazyvar_uploadedSurveysController:NSFetchedResultsController?=... 查看详情

Swift - NSFetchRequest 错误

】Swift-NSFetchRequest错误【英文标题】:Swift-NSFetchRequesterror【发布时间】:2015-06-1420:12:59【问题描述】:完整代码:importUIKitimportCoreDataclassInformationViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,NSFetchedResu 查看详情

Pod 安装错误后,Swift 3 结果出现编译错误

】Pod安装错误后,Swift3结果出现编译错误【英文标题】:Swift3resultswithcompileerrorsafterPodinstallerror【发布时间】:2016-09-2313:56:32【问题描述】:从Swift2.3迁移到Swift3后,出现以下编译错误:第一:错误:无法从“目标支持文件/Alamofi... 查看详情

将 swift 2.3 转换为 swift 3 错误

】将swift2.3转换为swift3错误【英文标题】:Convertswift2.3toswift3error【发布时间】:2016-12-1815:09:46【问题描述】:我在xcode8.2中将swift2.3转换为swift3swift2.3:有代码,有代码,有代码,有代码,有代码funcplayAudio()self.stopAudio()letlessonObjec... 查看详情

Swift 转换:错误 - UnsafeMutablePointer

】Swift转换:错误-UnsafeMutablePointer【英文标题】:Swiftconversion:ERROR-UnsafeMutablePointer【发布时间】:2017-01-0120:16:25【问题描述】:我正在尝试将我的Swift2代码转换为最新的语法(Swift3)。我收到以下错误:无法使用类型为“(UnsafeMut... 查看详情

转换 Swift2 -> Swift3:任何错误

】转换Swift2->Swift3:任何错误【英文标题】:ConversionSwift2->Swift3:ErrorswithAny【发布时间】:2017-01-1410:09:23【问题描述】:我正在使用来自https://github.com/brightec/CustomCollectionViewLayout的CustomCollectionViewLayout。在从Swift2转换到Swift3之... 查看详情

Swift 三元语法错误

】Swift三元语法错误【英文标题】:Swiftternarysyntaxerror【发布时间】:2015-05-0906:36:29【问题描述】:我以前一直使用Objective-C进行编程,而且我是Swift的新手。这个错误Xcode让我很困惑。funcrenderBufferAreaBAUp(yOffset:CGFloat,amount:CGFloat,ifL... 查看详情

从 swift 1.2 迁移后 swift2 中的 healthKit 错误

】从swift1.2迁移后swift2中的healthKit错误【英文标题】:healthKiterrorinswift2aftermigrationfromswift1.2【发布时间】:2015-08-0508:03:07【问题描述】:我有这段代码要求在Swift1.2工作的写权限,升级到Swift2.0后我收到一个奇怪的错误:...\'_\'isno... 查看详情

从 swift 1.2 迁移后 swift2 中的 healthKit 错误

】从swift1.2迁移后swift2中的healthKit错误【英文标题】:healthKiterrorinswift2aftermigrationfromswift1.2【发布时间】:2015-08-0508:03:07【问题描述】:我有这段代码要求在Swift1.2工作的写权限,升级到Swift2.0后我收到一个奇怪的错误:...\'_\'isno... 查看详情

使用 Swift 的 UICollectionView 错误

】使用Swift的UICollectionView错误【英文标题】:UICollectionViewerrorusingSwift【发布时间】:2014-06-1207:58:17【问题描述】:我收到此错误,尝试在Swift中使用UICollectionView:NSInternalInconsistencyException\',reason:\'attempttoregisteracellclasswhichisnotasubc... 查看详情

解析错误处理 swift 3

】解析错误处理swift3【英文标题】:ParseErrorHandlingswift3【发布时间】:2016-11-1721:47:03【问题描述】:在Parse文档中,我们可以在Swift中找到此会话错误处理。//SwiftclassParseErrorHandlingControllerclassfunchandleParseError(error:NSError)iferror.domain!... 查看详情