UITableViewCell contentView подпредставление нарушает ограничения AutoLayout - PullRequest
0 голосов
/ 06 ноября 2018

Я создаю табличное представление (только для кода)

private lazy var tableView: UITableView = {
    let tableView = UITableView()
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.register(MyCell.self, forCellReuseIdentifier: "MyCellIdentifier")
    return tableView
}()

Внутри клетки MyCell У меня есть подпредставление

private lazy var roundedContentView: UIView = {
    let view = UIView()
    view.backgroundColor = .red
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

Я пытаюсь настроить это представление (в инициализаторе), добавив его в качестве подпредставления для contentView

contentView.addSubview(roundedContentView)

NSLayoutConstraint.activate([
    roundedContentView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
    roundedContentView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
    roundedContentView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
    roundedContentView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
    roundedContentView.heightAnchor.constraint(equalToConstant: 200)
])

Хотя эта конфигурация приводит к нарушению ограничений

    [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:0x6000031284b0 V:|-(8)-[UIView:0x7fddedd41200]   (active, names: '|':UITableViewCellContentView:0x7fddedd03410 )>",
    "<NSLayoutConstraint:0x600003136670 UIView:0x7fddedd41200.bottom == UITableViewCellContentView:0x7fddedd03410.bottom - 8   (active)>",
    "<NSLayoutConstraint:0x600003137f70 UIView:0x7fddedd41200.height == 200   (active)>",
    "<NSLayoutConstraint:0x600003137660 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fddedd03410.height == 216   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003137f70 UIView:0x7fddedd41200.height == 200   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Интересен тот факт, что если я добавлю подпредставление как представление напрямую (addSubview(roundedContentView)), ограничения будут выполнены. Я попытался добавить приблизительную высоту строки, а также принудительно изменить вид макета, но ничего не происходит. Что я делаю не так?

...