Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何自定义UITableview右边框、上边框和下边框?_Ios_Swift_Iphone_Uitableview - Fatal编程技术网

Ios 如何自定义UITableview右边框、上边框和下边框?

Ios 如何自定义UITableview右边框、上边框和下边框?,ios,swift,iphone,uitableview,Ios,Swift,Iphone,Uitableview,如何在swift中的UITableview上使用颜色设置右边框、左边框、上边框和下边框 谢谢,请尝试以下完整边框: 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 这是底部边框: let border

如何在swift中的UITableview上使用颜色设置右边框、左边框、上边框和下边框


谢谢,

请尝试以下完整边框:

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
这是底部边框:

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
我将在某个时候开放我的扩展类的源代码

编辑:在这里,我更新了这里的函数

如果要使用颜色为tableview指定边框,请使用下面的代码 对于swift 3:


谢谢成功了!!还有一个问题,我们是否可以设置Uitableview的自动高度。以便根据数据增加或减少其高度?(抱歉,它与同一主题无关)非常感谢!它奏效了,我节省了很多研究时间;-)@亚历克斯:随时欢迎:)太好了!这很漂亮,谢谢你。也适用于Swift 3!我当然希望你能将所有复制粘贴的语句分解成一个实用方法。除了框架的大小之外,这四种方法都是相同的。找出如何编写一个实用方法,为边框的框架使用
CGRect
参数,然后使用它创建边框层并将其添加为子层。我尝试过使用它,但边框线向右突出,因为
addBorderTop
很奇怪,它到底有多少像素?我发现我添加了一些代码,以便在它开始工作之前将其放入viewdide中。viewDidLoad不工作。
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)
    }
}
yourTableView.layer.borderColor = UIColor.gray.cgColor
yourTableView.layer.borderWidth = 1.0