Чтение лога «Unable to simultaneously satisfy constraints»
UILabel закреплён к leading и trailing краям своего superview и вдобавок ему задана фиксированная ширина 200 pt. Во время выполнения консоль печатает лог ниже.
Прочитайте лог: определите конфликтующие constraint'ы и тот, который движок сломал для восстановления, и объясните, как связать эти адреса со своим исходником.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
(
"<NSLayoutConstraint:0x6000 UILabel:0x7fa8.width == 200>",
"<NSLayoutConstraint:0x6001 H:|-(16)-[UILabel:0x7fa8]>",
"<NSLayoutConstraint:0x6002 H:[UILabel:0x7fa8]-(16)-|>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000 UILabel:0x7fa8.width == 200>
Определите причину и назовите исправление.
Лог перечисляет конфликтующие constraint'ы и называет сломанный. Задайте каждому .identifier, чтобы связать адреса, и найдите переопределённую ось — ширину 200 pt против leading и trailing. Уберите один или понизьте приоритет.
- ✗Игнорируют предупреждение, потому что приложение всё же что-то рисует
- ✗Удаляют первый constraint из списка, не найдя настоящий конфликт
- ✗Оставляют constraint'ы безымянными, и адреса не связать с кодом
- →Чем помогает symbolic breakpoint на assertion раскладки поймать это вживую?
- →Почему понижение приоритета решает конфликт лучше удаления constraint?
The log names three constraints on the horizontal axis of one UILabel: a fixed width == 200, a leading pin, and a trailing pin. Leading + trailing already determine the width from the superview, so the extra fixed width over-constrains the axis — there is no single width that satisfies all three. Auto Layout keeps the required-priority pins and breaks the one it can, reported after "Will attempt to recover by breaking constraint".
Make the log readable, then remove the redundant constraint:
// 1. Name constraints so the log prints identifiers, not raw addresses.
let width = label.widthAnchor.constraint(equalToConstant: 200)
width.identifier = "label.fixedWidth"
// 2. The real fix: don't pin both edges AND a fixed width. Keep the edges…
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
])
// …or, if 200 pt is intentional, drop trailing and lower the width's priority
// so it yields when space is tight:
width.priority = .defaultHigh
Set a symbolic breakpoint on UIViewAlertForUnsatisfiableConstraints to pause the moment the conflict is logged and inspect the offending views in the debugger.