gpt4 book ai didi

ios - 如何找出与 NSLayoutConstraints 的确切冲突?

转载 作者:行者123 更新时间:2023-11-28 05:29:59 26 4
gpt4 key购买 nike

我正在尝试将我的 40x40 Logo 放置在屏幕顶部,距顶部 20pt,最后与父 View 水平对齐。

var views = ["tableView":self.tableView!,"logoImageView":logoImageView,"view":self.view]

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[logoImageView(40)]", options: nil, metrics: nil, views: views))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[logoImageView(40)]", options: nil, metrics: nil, views: views))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[logoImageView]", options: nil, metrics: nil, views: views))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view][logoImageView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views));

但这是我在调试器/控制台中遇到的错误。

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fa78487e8f0 V:|-(20)-[UIImageView:0x7fa784b29350] (Names: '|':UIView:0x7fa78373b8e0 )>",
"<NSLayoutConstraint:0x7fa784886f70 V:[UIView:0x7fa78373b8e0]-(0)-[UIImageView:0x7fa784b29350]>",
"<NSLayoutConstraint:0x7fa78488caa0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fa78373b8e0(667)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa784886f70 V:[UIView:0x7fa78373b8e0]-(0)-[UIImageView:0x7fa784b29350]>

如果有人可以帮助解释问题出在哪里以及执行 NSLayoutFormatOptions 的正确方法,那就太好了。

谢谢!

最佳答案

问题在于这两个约束:

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[logoImageView]", options: nil, metrics: nil, views: views))        
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view][logoImageView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views));

第一个说 logoImageView 的顶部应该距离其父 View 的顶部 20 点。第二个表示 logoImageView 的顶部距离 view 的底部应该是 0 点。

问题是 viewlogoImageView 的父 View 。你可以看出,因为这个约束中的 super View (|):

"<NSLayoutConstraint:0x7fa78487e8f0 V:|-(20)-[UIImageView:0x7fa784b29350]   (Names: '|':UIView:0x7fa78373b8e0 )>",

与此约束中的相邻 View 相同:

"<NSLayoutConstraint:0x7fa784886f70 V:[UIView:0x7fa78373b8e0]-(0)-[UIImageView:0x7fa784b29350]>",

因此,这两个约束共同要求 view 高度为 20 点。但是, TableView 为该 View 强加了 667 点的高度,如以下约束所示:

"<NSLayoutConstraint:0x7fa78488caa0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fa78373b8e0(667)]>"

因此产生了冲突。

我怀疑问题是您将 logoImageView 添加到了错误的 View 中,或者您打算在 V:[view][logoImageView] 视觉对象中列出一些其他 View 格式化字符串。

关于ios - 如何找出与 NSLayoutConstraints 的确切冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29162555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com