首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 11:“无法同时满足约束”

iOS 11:“无法同时满足约束”
EN

Stack Overflow用户
提问于 2018-02-09 12:51:01
回答 4查看 1.4K关注 0票数 1

始终运行FoodTracker教程;遵循以下步骤:“实现自定义控件”

编号/doc/uid/SW1 40015214-CH19-SW1

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

代码语言:javascript
运行
复制
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)

代码语言:javascript
运行
复制
// Add constraints  
button.translatesAutoresizingMaskIntoConstraints = false  
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true  
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true  

任何解决这个问题的暗示都是受欢迎的。

请注意我正在建立一个自定义控件..。我在谷歌上发现的所有注释似乎都不适用。

EN

回答 4

Stack Overflow用户

发布于 2018-02-11 14:08:35

结束语

这个问题自己消失了。

从摘录开始,Xcode IDE强调了视图设计中的一些关键点,其中我被邀请“点击图标”来修复;其中一个问题与冲突的对比有关。

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

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

这是我的想法,苹果必须更新它自己的在线文档,以避免人们失去理智,试图找出为什么事情不能像预期的那样工作。

票数 2
EN

Stack Overflow用户

发布于 2018-02-09 13:50:57

忽略我的前两条评论..。

在该教程中,您将添加自定义ratingControl作为垂直堆栈视图的排列子视图。这就是不需要约束的原因。

关键是使类@IBDesignable -它允许接口生成器和自动布局引擎使用类的内在大小,以适当的大小/位置/显示它。

(只是有点奇怪,这一点在本教程中没有提到。)

票数 1
EN

Stack Overflow用户

发布于 2020-03-17 06:15:04

我也有过同样的问题。此外,红色方块看起来比教程中显示的要大。为了使红色方块接受44大小的约束,我必须确保RatingControl位于堆栈视图的中,而不是在下面。

我附加图片来澄清我所说的“内部”堆栈视图的含义。

复制错误,请注意堆栈视图层次结构外的红色矩形。

错误修正后,红色矩形位于左侧的堆栈视图层次结构中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48706274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档