使用覆盖 func draw(_ rect: CGRect) 时显示奇怪的边框

Strange border shown when override func draw(_ rect: CGRect) is used

我遇到无法隐藏的奇怪和不需要的灰色边框问题。仅当我覆盖 func draw(_ rect: CGRect).

时才会出现灰色的顶部和右侧边框

出于测试原因,我特意在 layoutSubviews() 中为视图设置了蓝色。但是奇怪的边界仍然存在。

我创建了一个按钮:

import UIKit

class CardView: UIButton {
    override func draw(_ rect: CGRect) {         
        //activating this function enables the borders, even when I draw a Bezier path in this function the borders doesn't disappear    
    }
}

CardContainerView 中应显示 12 个按钮:

import UIKit

class CardContainerView: UIView {

    // Create Cards
    var cardViews = [CardView](){
        didSet {
            for card in cardViews {
                addSubview(card)
                card.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                card.layer.borderColor = UIColor.white.cgColor
                card.layer.borderWidth = 0
            }
        }
    }

    private(set) var grid = Grid(layout: Grid.Layout.aspectRatio(3/2))

    /// The centered rect in which the buttons are going to be positioned.
    private var centeredRect: CGRect {
        get {
            return CGRect(x: bounds.size.width * 0.025,
                          y: bounds.size.height * 0.025,
                          width: bounds.size.width * 0.95,
                          height: bounds.size.height * 0.95)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        grid.cellCount = cardViews.count
        grid.frame = centeredRect

        for i in 0...cardViews.count {
            if let frame = grid[i] {
                cardViews[i].frame = frame
                cardViews[i].layer.cornerRadius = 10
                cardViews[i].layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
                cardViews[i].layer.borderWidth = 0.0
            }
        }
    }
}

我已停用所有边框或使它们对 Card 和 CardContainaer 不可见,但它没有上述边框的效果。只有注释掉覆盖 func draw(_ rect: CGRect) 才能禁用边框。但是,我需要一个解决方案来绘制一些东西并隐藏边框。你有什么想法?如果您还需要网格对象,可以在此处找到:https://cs193p.stanford.edu/Fall2017/Grid.swift.zip

我找到问题了。我在我的情节提要中将我的 cardContainerView 约束之一的高度增加了 1。现在线条消失了。似乎是舍入错误或类似的原因导致了这个问题。