gpt4 book ai didi

ios - UIStackView 无法满足的自动布局约束

转载 作者:行者123 更新时间:2023-11-28 23:40:23 26 4
gpt4 key购买 nike

我在 Storyboard中定义了一个 UIStackView,第一个按钮的高度设置为 70,其他按钮的高度设置为 45。我收到此自动布局错误:

 [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:0x280f614f0 UIButton:0x10641a120.height == 45 (active)>",
"<NSLayoutConstraint:0x280f60e60 UIButton:0x106418f80.height == 70 (active)>",
"<NSLayoutConstraint:0x280f604b0 'UISV-alignment' UIButton:0x10641a120.bottom == UIButton:0x106418f80.bottom (active)>",
"<NSLayoutConstraint:0x280f63cf0 'UISV-alignment' UIButton:0x10641a120.top == UIButton:0x106418f80.top (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280f60e60 UIButton:0x106418f80.height == 70 (active)>

我知道 UIStackView 无法接受不同高度的 UIButton,这是否正确?让 UIStackView 接受其元素的不同高度或宽度的方法是什么?

最佳答案

堆栈 View 约束中的某些内容导致了问题。

这是一个有效的布局:

enter image description here

使用堆栈 View 属性:

enter image description here

通过代码添加第三个按钮之前的结果:

enter image description here

通过代码添加第三个按钮(高度限制为 60)后的结果:

enter image description here

没有自动布局警告或错误。

代码(连接到按钮 1)添加/删除 Button 3 作为堆栈 View 的排列 subview :

class TestViewController: UIViewController {

@IBOutlet var theStackView: UIStackView!

var thirdButton: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.setTitle("Button 3", for: .normal)
b.backgroundColor = .red
return b
}()

@IBAction func doAddThird(_ sender: Any) {

if theStackView.arrangedSubviews.count == 2 {

theStackView.addArrangedSubview(thirdButton)

} else {

if let v = theStackView.arrangedSubviews.last {
v.removeFromSuperview()
}

}

}

override func viewDidLoad() {
super.viewDidLoad()

// finish initializing the third button
if let v = theStackView.arrangedSubviews.first as? UIButton {
thirdButton.titleLabel?.font = v.titleLabel?.font
}

NSLayoutConstraint.activate([
thirdButton.heightAnchor.constraint(equalToConstant: 60),
])

}
}

关于ios - UIStackView 无法满足的自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53637026/

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