ios 如何自定义 UITableview 的右、上、下边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30531111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:07:13  来源:igfitidea点击:

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

iosswiftiphoneuitableview

提问by dhaval shah

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

如何快速在 UITableview 上设置带有颜色的右、左、上和下边框?

Thanks,

谢谢,

回答by Ashish Kakkad

Try this for full border:

试试这个全边框:

yourtable.layer.masksToBounds = true
yourtable.layer.borderColor = UIColor( red: 153/255, green: 153/255, blue:0/255, alpha: 1.0 ).CGColor
yourtable.layer.borderWidth = 2.0

This is for bottom border:

这是底部边框:

let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGrayColor().CGColor
border.frame = CGRect(x: 0, y: yourtable.frame.size.height - width, width:  yourtable.frame.size.width, height: yourtable.frame.size.height)

border.borderWidth = width
yourtable.layer.addSublayer(border)
yourtable.layer.masksToBounds = true

回答by Esqarrouth

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

编辑:给你,我在这里更新功能https://github.com/goktugyil/EZSwiftExtensions

回答by Kiran jadhav

if you want to give the border to tableview with color use below code for swift 3 :

如果你想用颜色为 tableview 提供边框,请使用下面的 swift 3 代码:

yourTableView.layer.borderColor = UIColor.gray.cgColor
yourTableView.layer.borderWidth = 1.0