如何给 UIView 添加底边框 Class?

How do add a bottom boarder to UIView Class?

我有一个视图 class,我将其应用于整个故事板中 UITableView 中的各种 UIView。

class ListContainerView: UIView {

override func awakeFromNib() {
    super.awakeFromNib()
    
    // Set the thickness of the border
    let thickness: CGFloat = 0.5
    
    // Set the color of the border
    let color: CGColor = UIColor.lightGray.cgColor
    
    // Add top border to the ListContainerView
    let topBorder = CALayer()
    topBorder.frame = CGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: thickness)
    topBorder.backgroundColor = color
    self.layer.addSublayer(topBorder)
    
    // Add bottom border to ListContainerView
    let bottomBorder = CALayer()
    bottomBorder.frame = CGRect(x: 0.0, y: self.frame.size.height - thickness, width: self.frame.size.width, height: thickness)
    bottomBorder.backgroundColor = color
    self.layer.addSublayer(bottomBorder)
    
}

}

截至目前,代码正确地向 UIView 添加了一个顶部边框。但是,它没有添加底部边框。我尝试了各种 Whosebug 答案中的许多策略,但 none 已经成功。我做错了什么?我该如何解决这个问题?

更新:我已经更正了我的代码中最初的拼写错误。此外,这里是访问此视图的示例的屏幕截图。在屏幕截图中,两个 Post 视图都分配了 class ListContainerView。

提前感谢您提供的任何帮助!

bottomBorder.frame = CGRect(x: 0.0, y: self.frame.size.width - thickness, width: self.frame.size.width, height: thickness)

不应该吗

bottomBorder.frame = CGRect(x: 0.0, y: self.frame.size.height - thickness, width: self.frame.size.width, height: thickness)

除非你的 UIView 是正方形

实际上,我有以下 UIView 个与您的类似的子类。尝试一下。它总是有效。

import UIKit

class TopBotView: UIView {
    override func draw(_ rect: CGRect) {
        let tHeight = self.frame.size.height
        let topLayer = CALayer()
        topLayer.frame = CGRect(x: 0.0, y: tHeight - 0.5, width: self.frame.size.width, height: 0.5)
        topLayer.backgroundColor = UIColor.white.withAlphaComponent(0.3).cgColor
        topLayer.masksToBounds = true
        self.layer.addSublayer(topLayer)
        
        let bottomLayer = CALayer()
        bottomLayer.frame = CGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: 0.5)
        bottomLayer.backgroundColor = UIColor.white.withAlphaComponent(0.3).cgColor
        bottomLayer.masksToBounds = true
        self.layer.addSublayer(bottomLayer)
    }
}

更新

如果你想用细线包围所有的边,我有下面的子类。

import UIKit

class AllAroundView: UIView {
    override func draw(_ rect: CGRect) {
        let tHeight = self.frame.size.height
        let topLayer = CALayer()
        topLayer.frame = CGRect(x: 0.0, y: tHeight - 0.5, width: self.frame.size.width, height: 0.5)
        topLayer.backgroundColor = UIColor.black.cgColor
        topLayer.masksToBounds = true
        self.layer.addSublayer(topLayer)
        
        let bottomLayer = CALayer()
        bottomLayer.frame = CGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: 0.5)
        bottomLayer.backgroundColor = UIColor.black.cgColor
        bottomLayer.masksToBounds = true
        self.layer.addSublayer(bottomLayer)
        
        let leftLayer = CALayer()
        leftLayer.frame = CGRect(x: 0.0, y: 0.0, width: 0.5, height: self.frame.size.height)
        leftLayer.backgroundColor = UIColor.black.cgColor
        leftLayer.masksToBounds = true
        self.layer.addSublayer(leftLayer)
        
        let rightLayer = CALayer()
        rightLayer.frame = CGRect(x: self.frame.size.width - 0.5, y: 0.0, width: 0.5, height: self.frame.size.height)
        rightLayer.backgroundColor = UIColor.black.cgColor
        rightLayer.masksToBounds = true
        self.layer.addSublayer(rightLayer)
    }
}