【问题标题】:Trying to present tableView试图呈现 tableView
【发布时间】:2018-05-12 03:10:52
【问题描述】:
  tableView = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width/2, height: view.frame.height/2))
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
    tableView.dataSource = self
    tableView.delegate = self
    view.addSubview(tableView)
    tableView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint(item: tableView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true

    NSLayoutConstraint(item: tableView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 50.0).isActive = true

我一直试图弄清楚如何在我的主视图上显示这个 UITableView,但它似乎不起作用。如果您知道该怎么做,请告诉我。提前谢谢你。

【问题讨论】:

  • 一旦你设置了translatesAutoresizingMaskIntoContraints = false,你的框架将不会被使用。为表格的宽度和高度添加NSLayoutConstraints。此外,您需要实现一些 tableView 委托和数据源方法。
  • 问题出在框架上。谢谢

标签: swift uitableview nslayoutconstraint


【解决方案1】:

当使用自动布局时,我们通常会实现以下约束之一:

  1. 引脚 4 边缘到超级视图
  2. 将宽度、高度设置为 superview 的固定数量或百分比,然后我们通过设置 x、y 或水平和垂直居中来确定原点。

所以我建议您对 TableView 的宽度和高度执行两个约束,而不是设置它的框架。

最后,不要忘记将它添加到您的根视图层次结构中,例如:self.view.addSubview(tableView)

【讨论】:

    猜你喜欢
    • 2018-11-20
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多