gpt4 book ai didi

ios - Swift 无法满足 Jump Right In 中的约束

转载 作者:行者123 更新时间:2023-11-30 12:07:18 24 4
gpt4 key购买 nike

我已经开始了 IOS Jump Right 在 swift 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)

这是在链接到水平堆栈 View 的“自定义”类中,我尝试将 self.autoresizeSubviews 设置为 false。设置 self.translateAutoresizingMaskIntoConstraints = false 确实会消除错误,但堆栈 View 的 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//以编程方式使用layoutAnchors定义
  • button.width = 44//以编程方式定义layoutAnchors的高度

然后你还有其他基于界面构建器的约束:

  • RatingControl.leading = UIButton.leading
  • 我认为带有按钮和评级控件的水平堆栈 View
  • RatingControl.with = 200

要避免此类错误,您需要:

  • 删除一项限制。您认为正在制造冲突的人

或者 - 通过降低优先级使其成为可选约束来更改约束

关于ios - Swift 无法满足 Jump Right In 中的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46524211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com