Swift 5-UI按钮在表中并排查看带有原始分隔线的页脚



我正试图在表视图页脚中以编程方式并排添加两个按钮

我遇到的问题是,在定义tableView页脚时,由于分隔线消失,我必须手动绘制分隔线。

如何简单地在表的左下角添加两个按钮查看页脚而不丢失原始分隔线?

var terms_button = UIButton()
var policy_button = UIButton()
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

//terms button
terms_button = UIButton(frame: CGRect(x: 70, y: 0, width: 100, height: 50))
terms_button.setTitle("Terms", for: .normal)
terms_button.setTitleColor(UIColor.black, for: .normal)
terms_button.titleLabel?.font = UIFont.roboto(size: 12, weight: .medium)
terms_button.titleLabel?.alpha = 0.38
terms_button.addTarget(self,action: #selector(didTapTermsButton),for: .touchUpInside)

//policy button
policy_button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
policy_button.setTitle("Privacy Policy", for: .normal)
policy_button.setTitleColor(UIColor.black, for: .normal)
policy_button.titleLabel?.font = UIFont.roboto(size: 12, weight: .medium)
policy_button.titleLabel?.alpha = 0.38
policy_button.addTarget(self,action: #selector(didTapPolicyButton),for: .touchUpInside)
let result = UIView()
// recreate insets from existing ones in the table view
let insets = tableView.separatorInset
let width = tableView.bounds.width - insets.left - insets.right
let sepFrame = CGRect(x: insets.left, y: -0.5, width: width, height: 0.5)
// create layer with separator, setting color
let sep = CALayer()
sep.frame = sepFrame
sep.backgroundColor = tableView.separatorColor?.cgColor
result.layer.addSublayer(sep)
result.addSubview(policy_button)
result.addSubview(terms_button)
return result
}

当您从viewForFooterInSection返回自己的let result = UIView()视图实例时,您将丢弃iOS提供的原始内置默认视图。

你可以尝试的是-

  1. 删除viewForFooterInSection实现
  2. 尝试使用iOS提供的默认内置视图
  3. 尝试自定义默认视图的外观,如下所示
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
guard let footerView = view as? UITableViewHeaderFooterView else { return }

let contentView = footerView.contentView
// Try adding your buttons to this `contentView` 
}

这是保持使用内置视图并进行可能的自定义的唯一方法。如果这在不同的iOS版本中不能可靠地工作,则需要返回viewForFooterInSection自定义视图实现。

相关内容

最新更新