【问题标题】:Objects cleared when adding subLayer to UIView将 subLayer 添加到 UIView 时清除的对象
【发布时间】:2015-08-16 13:56:42
【问题描述】:

我正在使用自定义 UIView 类,其中包含文本字段和标签,由 Storyboard 提供。当类在类中添加子层时,视图中包含的对象会消失,变得透明,或者可能变成与UIView 背景相同的颜色。是否有可以设置的属性或用于确保对象保持可见的方法?

提前致谢!

这是我的代码:

class viewBorder: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        // Create a rect with only 1x rounded corner
        let rectPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: UIRectCorner.TopLeft, cornerRadii: CGSize(width: 21.0, height: 21.0))
        let rectLayer = CAShapeLayer()
        rectLayer.shadowOffset = CGSize(width: -1.5, height: -1.5)
        rectLayer.shadowOpacity = 0.6
        rectLayer.path = rectPath.CGPath
        rectLayer.fillColor = tintColor.CGColor
        self.layer.addSublayer(rectLayer)
    }
}

【问题讨论】:

    标签: ios swift uiview swift2 cashapelayer


    【解决方案1】:

    试试这个:

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
    
        // Create a rect with only 1x rounded corner
        let rectPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: UIRectCorner.TopLeft, cornerRadii: CGSize(width: 21.0, height: 21.0))
        let rectLayer = CAShapeLayer()
        rectLayer.shadowOffset = CGSize(width: -1.5, height: -1.5)
        rectLayer.shadowOpacity = 0.6
        rectLayer.path = rectPath.CGPath
        rectLayer.fillColor = tintColor.CGColor
        rectLayer.zPosition = -1 // Added
        self.layer.addSublayer(rectLayer)
    }
    

    【讨论】:

    • 天啊!!!你是个天才!工作完美。我以前从来没有提到过'zPosition'!
    • 如果这解决了您的问题,请投票并接受它:)
    • 再次感谢@Bannings - 我试过投票,但由于我是新手,它没有公开显示(显然?)
    • 也许你的名声不够。不管怎样,没关系:)
    猜你喜欢
    • 2012-01-27
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 2015-12-19
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多