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

     2023-03-15     49

关键词:

【中文标题】视图层次结构没有为约束做好准备?【英文标题】:The view hierarchy is not prepared for the constraint? 【发布时间】:2015-08-12 19:51:15 【问题描述】:

我收到以下错误,即使我在添加按钮和应用约束之前设置了表格视图页脚。在-viewDidLoad-viewDidAppear 中设置和页脚视图后,我尝试调用以下方法。我似乎无法隔离问题。

视图层次结构没有为约束做好准备: 当添加到 视图,约束的项目必须是该视图的后代(或 查看自己)。如果需要解决约束,这将崩溃 在组装视图层次结构之前。打破 - [UIView _viewHierarchyUnpreparedForConstraint:] 进行调试。

2015-08-12 15:43:05.086 myApp[2293:299005] 未准备好查看层次结构 为约束。约束:容器层次结构:> | > 在容器层次结构中找不到视图: > 该视图的超级视图:;层 = ;内容偏移:0, 0;内容大小:800, 75>

这是我用来添加按钮和应用约束的代码:

- (void)addButtonContraints
//Create button
    _nextQuestionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_nextQuestionButton addTarget:self action:@selector(nextQuestion) forControlEvents:UIControlEventTouchUpInside];
    //_nextQuestionButton.frame = CGRectMake(0, 0, 200,40);
    [_nextQuestionButton setTitle:@"Next Question" forState:UIControlStateNormal];
    [_nextQuestionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _nextQuestionButton.backgroundColor = [UIColor whiteColor];
    _nextQuestionButton.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Next Question"
                                                                                    attributes:@NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f]];

    //Add shadow to button
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_nextQuestionButton.bounds];
    _nextQuestionButton.layer.masksToBounds = NO;
    _nextQuestionButton.layer.shadowColor = [UIColor blackColor].CGColor;
    _nextQuestionButton.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    _nextQuestionButton.layer.shadowOpacity = 0.25f;
    _nextQuestionButton.layer.shadowPath = shadowPath.CGPath;

    [_nextQuestionButton setTranslatesAutoresizingMaskIntoConstraints:NO];


    [self.quizTableView.tableFooterView addSubview:_nextQuestionButton];



    //Add contraints to button

    NSDictionary *dict = NSDictionaryOfVariableBindings(_nextQuestionButton);

    //width
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_nextQuestionButton(200)]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //height
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_nextQuestionButton(40)]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //right
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_nextQuestionButton]-40-|"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //top
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[_nextQuestionButton]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    _nextQuestionButton.hidden = YES;



【问题讨论】:

【参考方案1】:

您应该将最后两个约束添加到 self.quizTableView.tableFooterView,而不是 _nextQuestionButton。

【讨论】:

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

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

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

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

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

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

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

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

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

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

即使在添加子视图之后,视图层次结构也没有为约束做好准备

】即使在添加子视图之后,视图层次结构也没有为约束做好准备【英文标题】:ViewHierarchyNotPreparedforConstraintsEvenAfterAddingSubviews【发布时间】:2015-10-0610:02:18【问题描述】:我正在尝试实现一个集合视图,其中每个单元格都有可... 查看详情

为编程视图层次结构设置自动布局约束?

】为编程视图层次结构设置自动布局约束?【英文标题】:Settingupautolayoutconstraintsforprogrammaticviewhierarchy?【发布时间】:2013-06-2707:53:58【问题描述】:我正在像这样以编程方式创建视图层次结构:UIWindow*window=[[UIWindowalloc]initWithFra... 查看详情

错误:线程 1:“无法设置未准备好约束的视图层次结构的布局。”使用自动布局时

】错误:线程1:“无法设置未准备好约束的视图层次结构的布局。”使用自动布局时【英文标题】:Error:Thread1:"Impossibletosetuplayoutwithviewhierarchyunpreparedforconstraint."whenusingautolayout【发布时间】:2021-11-2318:30:04【问题描述】... 查看详情

视图层次结构未准备好

】视图层次结构未准备好【英文标题】:viewhierarchyisnotprepared【发布时间】:2016-02-2520:59:56【问题描述】:我是一名新手Swift开发人员,在一本优秀的iOS9Swift书籍中遵循代码示例。这本书刚刚演示了如何在视图层次结构的两个不... 查看详情

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

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

用于设置自动布局约束的 iOS 视图层次结构

】用于设置自动布局约束的iOS视图层次结构【英文标题】:iOSViewHierarchyforsettingupAutoLayoutConstraint【发布时间】:2013-01-1911:49:45【问题描述】:视图控制器中与self.navigationController.navigationBar和self.view的子视图最近的祖先是什么,以... 查看详情

iOS 程序约束:“在容器层次结构中找不到视图”

】iOS程序约束:“在容器层次结构中找不到视图”【英文标题】:iOSProgramaticConstraints:\'Viewnotfoundincontainerhierarchy\'【发布时间】:2015-01-2117:12:19【问题描述】:我的iOS自定义UITableViewCell需要以下布局:|查看基本信息|长长的矩形... 查看详情

约束或其锚点是不是引用不同视图层次结构中的项目?这是违法的

】约束或其锚点是不是引用不同视图层次结构中的项目?这是违法的【英文标题】:Doestheconstraintoritsanchorsreferenceitemsindifferentviewhierarchies?That\'sillegal约束或其锚点是否引用不同视图层次结构中的项目?这是违法的【发布时间】:... 查看详情

由于我的视图层次结构,显然没有调用 viewWillAppear

】由于我的视图层次结构,显然没有调用viewWillAppear【英文标题】:viewWillAppearnotcalledapparentlyduetomyviewhierarchy【发布时间】:2021-03-0102:44:22【问题描述】:这似乎是一个有据可查的问题,但在线解决方案没有奏效。以下是未能为... 查看详情

宽度约束更新后视图未重绘

】宽度约束更新后视图未重绘【英文标题】:Viewisnotredrawingafterwidthconstraintupdate【发布时间】:2016-12-2008:55:35【问题描述】:我有以下情况:UIImageView它的宽度需要在特定事件上增加。当我尝试增加它时,约束在增加,但视图没... 查看详情

数据库系统的结构抽象

...介质上的数据,含存储路径、存储方式、索引方式等三级视图ExternalSchema---(External)View外模式某一用户能够看到与处理的数据的结构描述(Conceptual)Schema——ConceptualView概念模式从全局角度理解/管理数据的结构描述,含相应的... 查看详情

为啥 Xcode 模拟器视图与视图层次结构视图不同

】为啥Xcode模拟器视图与视图层次结构视图不同【英文标题】:WhyXcodesimulatorviewisdifferentfromview-hierarchyview为什么Xcode模拟器视图与视图层次结构视图不同【发布时间】:2019-01-2002:10:57【问题描述】:我正在使用自动布局约束在UICol... 查看详情

有没有办法重置整个视图控制器层次结构?

】有没有办法重置整个视图控制器层次结构?【英文标题】:IsthereanywaytoresetthewholeviewControllerhierachy?【发布时间】:2016-09-0508:35:31【问题描述】:我有一个viewControllerA,它会被视图B(presented)推送和视图C(pushed)推送到viewController层... 查看详情