在UItableview ios swift中更改节标题的圆角半径

时间:2018-02-08 15:51:59

标签: ios uitableview cornerradius uitableviewsectionheader

我需要动态更改自定义UITableview节标题边角半径。对于所有角,最初节标题角半径为5。将单元格添加到剖面后,应删除左下角和右下角。我在自定义UITableViewHeaderFooter类的布局子视图方法中更改了它。但它没有反射,角落半径为所有角落添加,在添加细胞时不会改变。我的代码是:

   override func layoutSubviews() {
    super.layoutSubviews()
    textLabel?.textColor = UIColor.black
    contentView.backgroundColor = UIColor.clear
    backgroundView?.backgroundColor = UIColor.white
    if let sections = self.sections, let sectionIndex = self.sectionIndex, let values = sections[sectionIndex].values, values.count > 0, sections[sectionIndex].expanded {
        layer.cornerRadius = 5
        self.setRoundCorners(cornerRadius: 5, corners: [.topLeft, .topRight])
    } else {
        layer.cornerRadius = 5
        layer.mask = nil
    }
}

UIView扩展

func setRoundCorners(cornerRadius:CGFloat,corner:UIRectCorner){

    let path = UIBezierPath(roundedRect: bounds,
                            byRoundingCorners: corners,
                            cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.cgPath
    layer.mask = maskLayer
}

0 个答案:

没有答案