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

概述美好的一天,朋友们! 我没有尝试动画从代码中设置在IB中的视图.应用崩溃的原因如下: The view hierarchy is not prepared for the constraint… 我在这里看到了一些类似的问题,原因始终是以编程方式创建的视图尚未添加到superview中.但我在IB中创建了所有观点! 控制台还说: View not found in container hierar 美好的一天,朋友们!

我没有尝试动画从代码中设置在IB中的视图.应用崩溃的原因如下:

The vIEw hIErarchy is not prepared for the constraint…

我在这里看到了一些类似的问题,原因始终是以编程方式创建的视图尚未添加到supervIEw中.但我在IB中创建了所有观点!

控制台还说:

VIEw not found in container hIErarchy: (here goes it’s supervIEw)

它对我没有任何意义,因为事实上它是适当的supervIEw的子视图,xcode知道它 – 它立即打印视图层次结构,它适合.

可能是什么原因?
谢谢!

编辑:我使用的代码:

- (voID)setEditingConstraintsForVIEw:(UIVIEw *)vIEw{    // Pin given vIEw to top,fix it height    NSDictionary *givenVIEw = @{@"vIEw":vIEw};    vIEw.translatesautoresizingMaskIntoConstraints = NO;    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[vIEw]|" options:0 metrics:nil vIEws:givenVIEw];    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[vIEw(height)]" options:0 metrics:@{@"height":@(vIEwHeight)} vIEws:givenVIEw];    [self.animatedVIEw addSubvIEw:vIEw];    for (NSArray *constraints in @[horizontalConstraints,verticalConstraints]) {        [vIEw addConstraints:constraints];    }}

我还删除了在安装new之前在IB中设置的所有约束:

- (NSDictionary *)constraintsFromIB{    if (!_constraintsFromIB) {        _constraintsFromIB = @{@"vIEw1":self.vIEw1.referencingConstraintsInSupervIEws,@"vIEw2":self.vIEw2.referencingConstraintsInSupervIEws,@"vIEw3":self.vIEw3.referencingConstraintsInSupervIEws };    }    return _constraintsFromIB;}

然后:

- (voID)updateVIEwConstraints{    [super updateVIEwConstraints];    // clear all constraints    for (Nsstring *vIEwname in self.constraintsFromIB.allKeys) {        for (NSLayoutConstraint *constraint in self.constraintsFromIB[vIEwname]) {            [constraint remove];           }

}
}

更新2:方法我使用调用更改:当用户触摸视图时,调用此视图:

- (voID)animateConstraintsForState:(LDYEditingLabel)state{    self.editingLabel = state;            [UIVIEw animateWithDuration:0.3 animations:^{        [self updateVIEwConstraints];        [self.vIEw layoutIfNeeded];    }];}

稍后在updateVIEwConstraints中:有一个代码触发我的方法setEditingConstraintsForVIEw:(UIVIEw *)视图

解决方法@H_301_47@ 如果您使用IB创建约束,则可以将约束添加为出口.

然后更新约束并调用[self updateVIEwConstraints];在动画块中

总结

以上是内存溢出为你收集整理的iOS:以编程方式更改Interface Builder中设置的约束全部内容,希望文章能够帮你解决iOS:以编程方式更改Interface Builder中设置的约束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1109357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存