我已经启动了Swift IOS编程简介的iOS跳跃,我目前处于此阶段:[图像] [1]代码如下:
button.translatesAutoresizingMaskIntoConstraints = false;
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
这会产生以下错误:
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:0x604000091760 UIButton:0x7f7f95519110.width == 44 (active)
NSLayoutConstraint:0x600000097020 'UISV-canvas-connection' FoodTracker.RatingControl:0x7f7f95412d20.leading == UIButton:0x7f7f95519110.leading (active),
NSLayoutConstraint:0x600000093bf0 'UISV-canvas-connection' H:[UIButton:0x7f7f95519110]-(0)-| (active, names: '|':FoodTracker.RatingControl:0x7f7f95412d20 ),
NSLayoutConstraint:0x600000099fa0 'UIView-Encapsulated-Layout-Width' FoodTracker.RatingControl:0x7f7f95412d20.width == 200 (active)
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x604000091760 UIButton:0x7f7f95519110.width == 44 (active)
这是在链接到水平堆栈视图的"自定义"类中,我尝试设置self。设置self.translateautoresizingmaskIntoconstraints = false确实删除了错误,但是x,y堆栈视图的位置默认为0,0。如果:
,如何有约束冲突 button.translatesAutoresizingMaskIntoConstraints = false;
删除按钮上的所有约束。
编辑,完成代码:
class RatingControl: UIStackView {
//MARK: init
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
//MARK: Actions
func ratingButtonTapped(button: UIButton) {
print("HELLO WORLD!");
}
//MARK: private methods
private func setupButtons() {
// Create the button
let button = UIButton()
button.backgroundColor = UIColor.red
//self.translatesAutoresizingMaskIntoConstraints = false;
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false;
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
// Add the button to the stack
addArrangedSubview(button)
}
}
在这里,这种错误称为不满意的布局。基本上,对于您定义的约束,系统无法找到尊重您所有约束的有效解决方案。通过您发布的内容,我可以轻松确定这组约束:
- button.height = 44//用编程方式定义的 定义
- button.width = 44//定义的高度布局编程
然后,您还具有基于接口构建器的其他约束:
- ratingcontrol.heading = uibutton.Leading
- 我认为 的水平堆栈视图带有按钮和评分控件
- ratingcontrol.with = 200
要避免这种错误:
- 删除一个约束。您认为正在创建冲突的一种
或 - 通过降低其优先级
我在通过教程工作时遇到了类似的问题。
我意识到的是,在"实现自定义控件"一章中我的水平堆栈视图未嵌入在垂直堆栈视图中,该视图是在"构建更好的UI"一章中早些时候创建的。查看是否为您修复。