添加了子视图但丢失了部分绘图?
Subview added but lost part of drawing?
我想绘制一个具有 '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)
}
当我在故事板中将它用作单独的 UIView 时,它看起来不错。
但是,当我将它添加为自定义 UIButton 的子视图,然后在故事板中添加自定义按钮时,ellipse 消失并且背景变黑。
相关代码在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:布局我选择使用constraints,暂时只用frame来测试一下。所以,请不要提错误。
在将其添加到另一个视图之前,您应该将其背景颜色设置为透明颜色,否则,它看起来像是黑色背景颜色。
self.iconAdd.backgroundColor = UIColor.clearColor()
我想绘制一个具有 '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)
}
当我在故事板中将它用作单独的 UIView 时,它看起来不错。
但是,当我将它添加为自定义 UIButton 的子视图,然后在故事板中添加自定义按钮时,ellipse 消失并且背景变黑。
相关代码在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:布局我选择使用constraints,暂时只用frame来测试一下。所以,请不要提错误。
在将其添加到另一个视图之前,您应该将其背景颜色设置为透明颜色,否则,它看起来像是黑色背景颜色。
self.iconAdd.backgroundColor = UIColor.clearColor()