使用替代功能绘制(_ rect:CGRect)时显示的奇怪边框

时间:2019-01-21 19:05:53

标签: swift uiview drawrect

我有一个无法隐藏的奇怪和不需要的灰色边框问题。仅当我覆盖func draw(_ rect:CGRect)时,才会出现灰色的顶部和右侧边框。

Strange borders on top and right side of the button

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

When a border is set in layoutSubviews(), the strange borders persist

我创建了一个按钮:

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

1 个答案:

答案 0 :(得分:0)

我发现了问题。我将情节提要中的cardContainerView约束之一的高度增加了1。现在,行已消失。似乎是由于舍入错误或类似原因导致此问题。