【发布时间】:2018-02-09 12:51:01
【问题描述】:
总是跑到 FoodTracker 教程;遵循此步骤:“实现自定义控件”
当执行第一个检查点时,模拟器显示一个红色矩形而不是教程中指示的正方形;在我得到的调试窗格上:
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
欢迎任何解决问题的提示。
请注意,我正在构建一个自定义控件...我在谷歌上搜索到的所有注释似乎都不适用。
【问题讨论】:
-
@the4kman - 不幸的是没有……已经研究过了。请注意,Button 控件是通过编程方式创建的。
-
与我们分享您的代码
-
@MilanNosáľ - 认为这是一个完整的 Xcode 项目;可以在指定的链接中找到一份好的简历,而不会有错过发布重要内容的风险;我认为将按钮设置为 44 x 44 与视图中预定义的常量相冲突。例如,可以通过编程将宽度和高度约束优先级设置为 1001?
-
看起来您正在将按钮的边缘约束到其他元素,但随后您也试图约束宽度和高度。如果边缘最终不能准确地提供宽度和高度值,那将不起作用。
标签: ios cocoa-touch swift3 nslayoutconstraint