2016-09-30 110 views
0

我已经把一个正常的UIView的IB在我的UIViewController,并设置视图与下面的平局方法的UIView子类:框与透明区域一个UIView

override func draw(_ rect: CGRect) { 
    super.draw(rect) 

    self.isOpaque = false 
    self.backgroundColor?.setFill() 
    UIRectFill(rect) 
    let insetRect = self.bounds.insetBy(dx: 0, dy: 4) 
    UIColor.clear.setFill() 
    UIRectFill(insetRect) 
} 

预期的结果应该是后台颜色(白色)在视图的顶部和底部显示为一条线。这确实是发生了什么。但我想插入矩形是透明的,这就是为什么我设置颜色清除。但结果是黑色的。看到图片。

enter image description here

回答

0

我发现这样做,子类的UIView的另一种方式,这一次我得到了中央的预期透明度:

override init(frame: CGRect) { 
    super.init(frame: frame) 
    self.backgroundColor = UIColor.clear 
} 

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



override func draw(_ rect: CGRect) { 
    super.draw(rect) 

    let top = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.bounds.width, height: 4)) 
    UIColor.white.setFill() 
    top.fill() 
    let bottom = UIBezierPath(rect: CGRect(x: 0, y: self.bounds.height - 4, width: self.bounds.width, height: 4)) 
    bottom.fill() 
}