【发布时间】: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