How do I customize a UITableview right, top and bottom border?

前端 未结 3 1065
逝去的感伤
逝去的感伤 2020-12-25 15:29

How can I set right, left, top and bottom border with color on UITableview in swift?

Thanks,

3条回答
  •  遥遥无期
    2020-12-25 15:43

    extension UIView {
        func addBorderTop(size size: CGFloat, color: UIColor) {
            addBorderUtility(x: 0, y: 0, width: frame.width, height: size, color: color)
        }
        func addBorderBottom(size size: CGFloat, color: UIColor) {
            addBorderUtility(x: 0, y: frame.height - size, width: frame.width, height: size, color: color)
        }
        func addBorderLeft(size size: CGFloat, color: UIColor) {
            addBorderUtility(x: 0, y: 0, width: size, height: frame.height, color: color)
        }
        func addBorderRight(size size: CGFloat, color: UIColor) {
            addBorderUtility(x: frame.width - size, y: 0, width: size, height: frame.height, color: color)
        }
        private func addBorderUtility(x x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: UIColor) {
            let border = CALayer()
            border.backgroundColor = color.CGColor
            border.frame = CGRect(x: x, y: y, width: width, height: height)
            layer.addSublayer(border)
        }
    }
    

    I am going to open source my extension classes at some point.

    Edit: Here you go, I update the functions in here https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题