iOS 11 : "Unable to simultaneously satisfy constraints"

标签 ios cocoa-touch swift3 nslayoutconstraint

总是遇到 FoodTracker 教程;遵循此步骤:“实现自定义控件”

https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1

并且在执行第一个检查点时,模拟器显示一个红色矩形而不是教程中所示的正方形;在调试 Pane 上,我得到:

2018-02-09 11:19:42.130595+0100 FoodTracker[7439:80369] [MC] Lazy loading NSBundle MobileCoreServices.framework  
2018-02-09 11:19:42.131628+0100 FoodTracker[7439:80369] [MC] Loaded MobileCoreServices.framework  
2018-02-09 11:19:42.165143+0100 FoodTracker[7439:80369] [LayoutConstraints] 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.  
(  
    "<NSLayoutConstraint:0x60400028a3c0 UIButton:0x7f8a0bd0e330.width == 44   (active)>",  
    "<NSLayoutConstraint:0x60400028cda0 'UISV-canvas-connection' FoodTracker.RatingControl:0x7f8a0bd08890.leading == UIButton:0x7f8a0bd0e330.leading   (active)>",  
    "<NSLayoutConstraint:0x60400028ce40 'UISV-canvas-connection' H:[UIButton:0x7f8a0bd0e330]-(0)-|   (active, names: '|':FoodTracker.RatingControl:0x7f8a0bd08890 )>",  
    "<NSLayoutConstraint:0x60400028c940 'UIView-Encapsulated-Layout-Width' FoodTracker.RatingControl:0x7f8a0bd08890.width == 200   (active)>"  
)  

Will attempt to recover by breaking constraint  
<NSLayoutConstraint:0x60400028a3c0 UIButton:0x7f8a0bd0e330.width == 44   (active)>  

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.  
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.  
2018-02-09 11:19:42.165949+0100 FoodTracker[7439:80369] [LayoutConstraints] 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.  
(  
    "<NSLayoutConstraint:0x60400028a370 UIButton:0x7f8a0bd0e330.height == 44   (active)>",  
    "<NSLayoutConstraint:0x60400028a320 'UISV-canvas-connection' FoodTracker.RatingControl:0x7f8a0bd08890.top == UIButton:0x7f8a0bd0e330.top   (active)>",  
    "<NSLayoutConstraint:0x60400028cf30 'UISV-canvas-connection' V:[UIButton:0x7f8a0bd0e330]-(0)-|   (active, names: '|':FoodTracker.RatingControl:0x7f8a0bd08890 )>",  
    "<NSLayoutConstraint:0x60400028c990 'UIView-Encapsulated-Layout-Height' FoodTracker.RatingControl:0x7f8a0bd08890.height == 110   (active)>"  
)  

Will attempt to recover by breaking constraint  
<NSLayoutConstraint:0x60400028a370 UIButton:0x7f8a0bd0e330.height == 44   (active)>  

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.  
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.  

我认为以编程方式创建按钮并在此处应用自定义约束时会发生一些变化:(我正在使用 Swift 和 Xcode 9)
// Add constraints  
button.translatesAutoresizingMaskIntoConstraints = false  
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true  
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true  

欢迎任何解决问题的提示。

请注意,我正在构建一个自定义控件......我发现谷歌搜索的所有笔记似乎都不适用。

最佳答案

结语

这个问题自己消失了。

继续练习,Xcode IDE 强调了 View 设计中的一些关键点,我被邀请“单击图标”进行修复;其中一个问题与相互冲突的约束有关。

正如#DonMag 所建议的,RatingControl 类必须标记为@IBDesignable(这个装饰器将在后面的练习中添加)让Xcode 立即查看自定义控件的最终结果。几乎:星星没有出现,并且控件不会自动缩放将星星添加到控件属性。直到我关闭,再次打开并重建了整个项目几次。

教程(为 Swift 3.2 编写)和实际 Xcode(在 Swift 4 中打开的默认项目适用)的差异。

我的想法是,Apple 必须更新它自己的在线文档,以避免人们在试图弄清楚为什么事情不像预期的那样工作时失去理智。

关于iOS 11 : "Unable to simultaneously satisfy constraints",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706274/

相关文章:

cocoa-touch - 使用 CGContext* 函数时出现“无效上下文 0x0”错误

ios - 我想在 swift3 的 tableview 中插入行作为部分

ios - 如何在ios中创建透明标签栏

iOS objective-C NSData 到 NSString 返回 nil,如何忽略无效的 UTF-8

ios - 如何获取 QLPreviewItem 的文档图标?

objective-c - 单击按钮后禁用按钮

javascript - 如何在WKWebView中调用带参数/不带参数的JavaScript函数?

ios - swift 3 : nil when trying to get email from Twitter using Fabric

ios - CVBuffer 和 CVImageBuffer 之间的关系是什么

ios - 与传递对象保持循环