我已经启动了IOS Jump Right在快速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.autoresizeSubviews设置为false。设置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)
}
}
答案 0 :(得分:0)
这种错误称为不满意的布局。基本上,对于您定义的约束,系统无法找到符合所有约束条件的有效解决方案。通过你发布的内容,我可以很容易地确定这组约束:
然后你还有其他基于Interface builder的约束:
要避免此类错误,您需要:
OR - 通过降低优先级使其成为可选项来更改约束
答案 1 :(得分:0)
在学习本教程时,我遇到了类似的问题。
我意识到,“实施自定义控件”一章中的水平堆栈视图没有嵌入在“构建更好的UI”一章中先前创建的“垂直堆栈视图”中。看看是否能为您解决问题。