【问题标题】:Fixing breaking constraint iPhone Plus only仅修复破坏约束 iPhone Plus
【发布时间】:2017-12-26 16:22:49
【问题描述】:

我有 tableViewCell,它有 UIButton。我设置了这样的约束:

make.left.equalTo(contentView.snp.left).offset(16)
                make.right.equalTo(contentView.snp.right).offset(-16)
                make.bottom.equalTo(contentView.snp.bottom).offset(-46)
                make.top.equalTo(contentView.snp.top).offset(24)
                make.height.equalTo(64)

它确实显示了具有正确高度的按钮并设置了正确的顶部和底部偏移量,但它会在控制台中打印错误日志:

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. 
(
    "<SnapKit.LayoutConstraint:0x6000002a34e0@RoundedButtonCell.swift#49 POS2.MSButton:0x7faf22c674b0.bottom == UITableViewCellContentView:0x7faf22c672c0.bottom - 46.0>",
    "<SnapKit.LayoutConstraint:0x6000000b8600@RoundedButtonCell.swift#50 POS2.MSButton:0x7faf22c674b0.top == UITableViewCellContentView:0x7faf22c672c0.top + 24.0>",
    "<SnapKit.LayoutConstraint:0x6000002a3de0@RoundedButtonCell.swift#51 POS2.MSButton:0x7faf22c674b0.height == 64.0>",
    "<NSLayoutConstraint:0x604000484420 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7faf22c672c0.height == 134   (active)>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x6000002a3de0@RoundedButtonCell.swift#51 POS2.MSButton:0x7faf22c674b0.height == 64.0>

我想它以某种方式“不喜欢”我将 UIButton 高度设置为常数,并设置顶部和底部偏移量。如何解决?

PS。我的控制器是 UITableViewController,我这样配置 tableView:

private func setupTable() {
        tableView.estimatedRowHeight = 40
        tableView.rowHeight = UITableViewAutomaticDimension

        tableView.separatorInset = .zero
        tableView.separatorColor = .clear

        tableView.keyboardDismissMode = .onDrag

        tableView.tableFooterView = nil
        tableView.tableHeaderView = nil

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(RadioButtonCell.self, forCellReuseIdentifier: MSTableViewCellType.radioButtonCell.rawValue)
        tableView.register(InputViewCell.self, forCellReuseIdentifier: MSTableViewCellType.inputViewCell.rawValue)
        tableView.register(CommentViewCell.self, forCellReuseIdentifier: MSTableViewCellType.commentViewCell.rawValue)
        tableView.register(RoundedButtonCell.self, forCellReuseIdentifier: MSTableViewCellType.roundedButtonViewCell.rawValue)
    }

setupTableviewDidLoad 中被调用

在 cellForRow 中:

  case .roundedButton:
            let cell = tableView.dequeueReusableCell(withIdentifier: MSTableViewCellType.roundedButtonViewCell.rawValue, for: indexPath) as! RoundedButtonCell
            guard let model = fieldModel as? TextContainViewModel else { return UITableViewCell()}
            cell.setup(title: model.title)
            cell.contentView.layoutMargins = UIEdgeInsets(top: 24, left: 16, bottom: 46, right: 16)
            cell.layoutIfNeeded()

            return cell
        }

我没有覆盖 heightForRowAtIndexPath。

【问题讨论】:

标签: ios swift autolayout


【解决方案1】:

问题是您已经明确设置了单元格(行)的高度。您不能明确规定行高,也不能提供一整套完整的优先级垂直约束。做一个或另一个。要么明确地调整行大小并提供不完整的约束,要么提供完整的约束并告诉行自动调整大小。

【讨论】:

  • 我怎样才能提供完整的约束?我希望我的视图是固定高度,我需要顶部和底部偏移。
  • 您希望 what 视图固定高度?按钮还是单元格(行)?选择一个。这就是我的回答告诉你的。尝试省略make.height.equalTo(64) 这一行。会发生什么?
  • 我不希望单元格是固定高度我希望它是顶部偏移 + 视图高度 + 机器人偏移高度。请帮我修改我的代码以修复警告
  • 我不想明确设置单元格高度。如果我这样做,我不是故意的。请告诉我如何使单元格高度动态化
  • 将表格视图的估计行高设置为某个值(例如 140),并将表格视图的实际行高设置为 UITableViewAutomaticDimension。您可以对整个表格视图执行此操作,也可以使用 heightForRowAt 逐行执行此操作。
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
相关资源
最近更新 更多