始终进入 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
欢迎提出解决问题的任何提示。
请注意我正在构建一个自定义控件...我发现谷歌搜索的所有笔记似乎都不适用。
答案 0 :(得分:1)
忽略我的前两条评论......
在该教程中,您将自定义ratingControl
添加为垂直堆栈视图的已排列子视图。这就是为什么不需要约束的原因。
关键是使类@IBDesignable
- 允许Interface Builder和Auto-Layout引擎使用类'内在大小来正确地调整大小/位置/显示它。
(在教程中没有注意到,有点令人惊讶。)
答案 1 :(得分:1)
这个问题自己消失了。
继续练习,Xcode IDE在视图设计中强调了一些关键点,我被邀请参加&#34;点击图标&#34;修理;其中一个问题与冲突的禁区有关。
正如#DonMag所建议的那样,必须将RatingControl类标记为@IBDesignable(稍后将在练习中添加此装饰器)以让Xcode立即查看自定义控件的最终结果。几乎:星星没有出现,并且控件不会自动缩放添加星星到控制属性。直到我关闭,再次打开并重建整个项目几次。
教程(为Swift 3.2编写)和实际Xcode(在Swift 4中打开的默认项目)的差异适用。
我的想法是,Apple必须更新它自己的在线文档,以避免人们放松心情,试图弄清楚为什么事情不会像预期的那样发挥作用。
答案 2 :(得分:0)
我有同样的问题。 此外,红色方块看起来比教程中显示的要大。 为了使红色正方形能够接受大小为44的约束,我必须确保RatingControl位于堆栈视图的内部内部,而不是下方。
我要附加图像以阐明“在堆栈视图内部”的含义。
error reproduced, please note red rectangle outside the stack view hierarchy
error fixed, the red rectangle is inside the stack view hierarchy on the left
答案 3 :(得分:0)
发生此问题是因为所有子视图的总宽度不等于StackView的宽度。
使用此计算可以解决问题:
let width = (self.frame.width - self.spacing * CGFloat(starCount-1))/CGFloat(starCount)
let height = self.frame.height
............
for _ in 0..<starCount {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor.red
button.heightAnchor.constraint(equalToConstant: height).isActive = true
button.widthAnchor.constraint(equalToConstant: width).isActive = true
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
addArrangedSubview(button)
}