简体   繁体   English

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

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

How can I set right, left, top and bottom border with color on UITableview in swift? 如何在UITableview中快速设置右边,左边,上边和下边框的颜色?

Thanks, 谢谢,

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
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

if you want to give the border to tableview with color use below code for swift 3 : 如果你想使用下面代码为swift 3的颜色使用边框到tableview:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除UITableView顶部和底部的阴影? - How do I remove the shadows on the top and bottom of UITableView? 在UITableView中,隐藏行之后如何关闭行的底部边框? - In a UITableView, how do I close the bottom border of a row after hiding the row after it? 我知道如何仅在顶部禁用 uitableview 的弹跳 - 但我怎样才能仅在底部执行呢? - I know how to disable bouncing of uitableview on top only - but how can I do it only on the bottom? 如何以编程方式使UITableView的第一部分显示在距顶部边框一定距离的地方? - How do I make the first section of UITableView appear at a certain distance from the top border programmatically? Swift 3-如何在聊天应用程序的UITableView中从下至上进行页面显示? - Swift 3 - How I Do Page From Bottom To Top In UITableView In A Chat App? 如何绘制在顶部和右下方的对象周围流动的文本? - How do I draw text which flows around objects at the top and bottom right? 分组UITableView底部边框 - Grouped UITableView bottom border 如何使用当前设置将uitableview定制为不同的部分? - How do I customize a uitableview into different sections with my current setup? 如何在UITableView中仅设置右边框? - How to set only right border in UITableView? 如何将UITableView的顶部粘贴到UISearchBar的底部? - How to stick top of the UITableView to the bottom of the UISearchBar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM