Subview添加但丢失了部分绘图?

时间:2015-02-06 07:09:39

标签: ios uiview uibutton core-graphics drawrect

我想绘制一个带有'add'表面和UIBlurEffect背景的自定义UIButton。我在表面视图中覆盖了drawRect方法:

class SurfaceAdd: UIView {
    override func drawRect(rect: CGRect) {
    var currentContext = UIGraphicsGetCurrentContext()
    CGContextSaveGState(currentContext)
    //draw circle
    CGContextAddEllipseInRect(currentContext, self.bounds)
    CGContextSetRGBFillColor(currentContext, 0, 0, 0, 0.2)
    CGContextFillPath(currentContext)

    CGContextRestoreGState(currentContext)
    CGContextSetLineCap(currentContext, kCGLineCapRound)
    CGContextSetLineWidth(currentContext, 8)
    CGContextSetRGBStrokeColor(currentContext, 1, 1, 1, 1)
    let height = self.frame.size.height
    let width = self.frame.size.width
    //draw +
    CGContextSetShadow(currentContext, CGSizeMake(1, 1), 0)
    CGContextMoveToPoint(currentContext, width*0.25, height/2)
    CGContextAddLineToPoint(currentContext, width*0.75, height/2)
    CGContextMoveToPoint(currentContext, width/2, height*0.25)
    CGContextAddLineToPoint(currentContext, width/2, height*0.75)
    CGContextStrokePath(currentContext)
}

enter image description here

当我在故事板中将其用作单独的UIView时,它看起来很不错 但是,当我将其添加为我的自定义UIButton的子视图,然后在故事板中添加自定义按钮时,椭圆消失,背景变为黑色。

enter image description here

CustomButton中的相关代码:

var iconAdd:SurfaceAdd!
override init() {
    super.init()
    setup()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}
private func setup(){
    self.iconAdd = SurfaceAdd(frame: self.bounds)
    self.addSubview(iconAdd)
    self.setNeedsDisplay()
}

我怎么了?

ps:我选择使用约束进行布局,现在我只使用frame进行测试。所以,请不要提到错误。

1 个答案:

答案 0 :(得分:0)

在将背景颜色添加到另一个视图之前,应将其背景颜色设置为清晰颜色,否则,它看起来应该具有黑色背景颜色。

self.iconAdd.backgroundColor = UIColor.clearColor()
相关问题