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

标签 ios swift autolayout

我正在尝试将我的 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/

相关文章:

ios - Localizable.strings 不工作

ios - Swift BLE "didDiscoverServices"未执行。我错过了什么吗?

iOS 10 中的自动布局增加高度并使 Y 位置为负

iphone - UIView 子类中的事件处理

ios - 有没有办法强制 iOS *不*来自浏览器的深度/通用链接?

ios - ViewController 未从内存中释放

ios - 围绕中心点旋转 CGPoint

ios - 多次将完全相同的 NSLayoutConstraints 附加到同一个 UIView 会发生什么?

ios - UITableView 内的动态 UIImageView 大小

与 Lynda 一起学习 iOS : Getting error can't invoke 'init' with argument of type @lvalue String