Reputation: 8875
I want to draw a custom UIButton with 'add' surface and UIBlurEffect background. I override the drawRect method in the surface view:
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)
}
It looks nice when I use it as a separate UIView in the storyboard.
However, when i add it as a subview of my custom UIButton, and then add the custom button in storyboard, the ellipse disappear and the background turns black.
relevant code in CustomButton
here:
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()
}
What's wrong with me?
ps:I choose to use constraints for layout, for now I just use frame to have a test. So, please don't mention mistake about that.
Upvotes: 0
Views: 93
Reputation: 13766
You should set its background color as clear color before it being added to another view, otherwise, it will look like it has a black background color.
self.iconAdd.backgroundColor = UIColor.clearColor()
Upvotes: 0