我使用viewForHeaderInSection放置添加按钮,如下所示:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let frame: CGRect = tableView.frame
let addButton: UIButton = UIButton(frame: CGRectMake(70, 0, 70, 70))
addButton.setTitle("+", for: .normal)
addButton.layer.cornerRadius = 8
addButton.backgroundColor = .blue
let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.height, frame.size.width))
headerView.addSubview(addButton)
addButton.addConstraints(.alignTrailingWithTrailing(of: headerView, const: 10))
return headerView
}
不幸的是,这使得我的UITable视图看起来不太好:
如何将添加按钮放置到标题部分?(“联系人”标题的权利)。
谢谢。视图层次结构已附加。
1条答案
按热度按时间hk8txs481#
似乎没有办法在UITableViewController中实现它。
但还有另一种方法-您可以创建UIViewController,使其符合TableView协议,并使用添加按钮实现UITableView和UIView。
我是这样做的:
它按预期工作: