ios – 以编程方式添加某些约束时的NSGenericException

ios – 以编程方式添加某些约束时的NSGenericException,第1张

概述这是我的代码 func showMessageView(){ let leftConstraint = messageView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor) let bottomConstraint = messageView.bottomAnchor.constraintGreaterThanOr 这是我的代码

func showMessageVIEw(){    let leftConstraint = messageVIEw.leftAnchor.constraintEqualToAnchor(self.vIEw.leftAnchor)    let bottomConstraint = messageVIEw.bottomAnchor.constraintGreaterThanorEqualToAnchor(self.vIEw.bottomAnchor)    let highConstratin = messageVIEw.heightAnchor.constraintEqualToConstant(44.0)    let wIDthConstraint = messageVIEw.wIDthAnchor.constraintGreaterThanorEqualToAnchor(self.vIEw.wIDthAnchor)    NSLayoutConstraint.activateConstraints([leftConstraint,bottomConstraint,highConstratin,wIDthConstraint])    self.vIEw.translatesautoresizingMaskIntoConstraints = false    self.vIEw.addSubvIEw(messageVIEw)}

我收到这个例外:

Terminating app due to uncaught exception ‘NSGenericException’,reason: ‘Unable to activate constraint with items <UIVIEw: 0x7fe62e00be70; frame = (0 0; 240 128); autoresize = RM+BM; layer = <CALayer: 0x7fe62e06c8f0>> and <UItableVIEw: 0x7fe62c80be00; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fe62b6a54f0>; layer = <CALayer: 0x7fe62b53aba0>; contentOffset: {0,-64}; contentSize: {375,658}> because they have no common ancestor. Does the constraint reference items in different vIEw hIErarchIEs? That’s illegal.’

解决方法 我只是首先将messageVIEw添加到视图中解决了我的问题,如下所示:

func showMessageVIEw(){        self.vIEw.addSubvIEw(messageVIEw)        let leftConstraint = messageVIEw.leftAnchor.constraintEqualToAnchor(self.vIEw.leftAnchor)        let bottomConstraint = messageVIEw.bottomAnchor.constraintGreaterThanorEqualToAnchor(self.vIEw.bottomAnchor)        let highConstratin = messageVIEw.heightAnchor.constraintEqualToConstant(44.0)        let wIDthConstraint = messageVIEw.wIDthAnchor.constraintGreaterThanorEqualToAnchor(self.vIEw.wIDthAnchor)        NSLayoutConstraint.activateConstraints([leftConstraint,wIDthConstraint])        self.vIEw.translatesautoresizingMaskIntoConstraints = false}
总结

以上是内存溢出为你收集整理的ios – 以编程方式添加某些约束时的NSGenericException全部内容,希望文章能够帮你解决ios – 以编程方式添加某些约束时的NSGenericException所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1048237.html

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

发表评论

登录后才能评论

评论列表(0条)

保存