2016-09-30 109 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() 
}