【问题标题】:Autolayout broken in Xcode-9 betaXcode-9 beta 中的自动布局被破坏
【发布时间】:2017-11-25 12:35:19
【问题描述】:

我的一个屏幕中有UITableView。它显示具有动态高度的部分标题和行。
当我从 Xcode8.3 迁移到 Xcode-9 beta 时,此设置存在两个问题。

1 标题视图高度损坏
2 断行的动态高度

我已将表格视图设置为:

然后我有自定义单元格



代码如下:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return //dynamically calculated height according to msg text
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerHeight:CGFloat = tableView.sectionHeaderHeight
    let headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: tableView.width, height: headerHeight), withHeaderLable: "Date/Day of msg")
    return headerView
}

即使有多行消息,这在 Xcode8.3 中也能完美运行,但在 Xcode9 beta 中被破坏为: 当我在 Xcode9 beta 中使用 Swift3.2 设置运行时:

当我在 Xcode9 beta 中使用 Swift4 设置运行时:

这种行为的原因是什么?

【问题讨论】:

  • 你为什么使用测试版?
  • @elk_cloner 这应该是个问题,为什么要使用测试版。这是一项挑战即将到来的技术的举措。如果这真的是苹果的bug,那么面对这样的挑战并找到合适的解决方案对我们会有帮助

标签: ios uitableview autolayout row-height xcode9-beta


【解决方案1】:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30.0

将此添加到您的 viewDidLoad 中,并且不需要 tableview 委托方法 estimatedHeightForRowAt

【讨论】:

  • 仍然想知道为什么它在 xcode9 中而不是在 xcode8.3 中需要这个!
  • 其实header的问题与自动布局无关,与NSFetchedResultsController有关; here's question about that
猜你喜欢
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 2021-07-14
  • 2019-01-27
相关资源
最近更新 更多