如何在 iOS 13.X 中以编程方式修复此约束?
How do I fix this constraint programmatically in iOS 13.X?
我想在我的应用程序中添加一个水平堆栈视图,这样我就可以有一个图像评分机制。
我在 StackView
class 中创建了一个私有方法,它将在我的堆栈视图中生成尺寸为 44x44 的 UIButton
,但似乎 UIButton
填充在整个堆栈视图中。
我确保以编程方式在我的私有方法中为 UIButton
创建约束,并尝试在故事板窗格中调整约束,但无济于事。我在下面附上了我所指内容的图片。
如有任何帮助,我们将不胜感激!
发生这种情况是因为您为 UIButton
设置的约束使其无法满足冲突约束。
Xcode指定Debugger中这段代码下的冲突约束:
[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.
一种可能的解决方法是将您的 UIButton
添加到 UIView
:
let button = UIButton()
button.backgroundColor = .red
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
let viewButton = UIView()
viewButton.addSubview(button)
stack.addArrangedSubview(viewButton)
这会导致类似这样的结果(对于这个例子,我将 viewButton.backgroundColor
设置为灰色 ):
Screenshot
我想在我的应用程序中添加一个水平堆栈视图,这样我就可以有一个图像评分机制。
我在 StackView
class 中创建了一个私有方法,它将在我的堆栈视图中生成尺寸为 44x44 的 UIButton
,但似乎 UIButton
填充在整个堆栈视图中。
我确保以编程方式在我的私有方法中为 UIButton
创建约束,并尝试在故事板窗格中调整约束,但无济于事。我在下面附上了我所指内容的图片。
如有任何帮助,我们将不胜感激!
发生这种情况是因为您为 UIButton
设置的约束使其无法满足冲突约束。
Xcode指定Debugger中这段代码下的冲突约束:
[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.
一种可能的解决方法是将您的 UIButton
添加到 UIView
:
let button = UIButton()
button.backgroundColor = .red
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
let viewButton = UIView()
viewButton.addSubview(button)
stack.addArrangedSubview(viewButton)
这会导致类似这样的结果(对于这个例子,我将 viewButton.backgroundColor
设置为灰色 ):
Screenshot