自定义tableview标头的左上角和右上角

时间:2019-10-20 00:19:12

标签: ios swift uitableview

我想以编程方式在圆桌视图标题的左上角和右上角添加圆角。

This the app I am working on The corners I want to add my app

2 个答案:

答案 0 :(得分:0)

您可以通过实现willDisplayHeaderView函数来做到这一点:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            let headerView = view as! UITableViewHeaderFooterView
            headerView.contentView.backgroundColor = UIColor.lightGray
            headerView.contentView.layer.cornerRadius = 20
            headerView.contentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}

答案 1 :(得分:0)

尝试添加extension

extension UIView {

    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

使用:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
        let header = cell as! HeaderView

        header.contentView.roundCorners([.topLeft, .topRight], radius: 100)

        return cell
}
相关问题