2015-06-20 77 views
3

在我的ViewController的主要NSView中,我重寫了func drawRect(dirtyRect: NSRect)方法來在我的主視圖上使用NSBezierPath實現圓角。 在同樣的方法中,我也指定了我的主視圖的漸變背景。在使用NSBezierPath的NSView上的圓角被嚴重繪製

override func drawRect(dirtyRect: NSRect) { 

    let path: NSBezierPath = NSBezierPath(roundedRect: self.bounds, xRadius: 18.0, yRadius: 18.0) 
    path.addClip() 

    let gradient = NSGradient(startingColor: NSColor(hexColorCode: "#383838"), endingColor: NSColor(hexColorCode: "#222222")) 
    gradient.drawInRect(self.frame, angle: 90) 
} 

即產生下列圖像中示出的問題:

該圖像顯示的觀點角中的一個。圓角的圓角只是部分成功,因爲仍然有一個白色的角落伸出窗外的圓角。

如果有人有更好的方法來設置窗口的角半徑,我會接受這樣的建議。我已經做了大量的研究,但是這個解決方案似乎是最簡單的。

有關如何解決此問題的任何建議,我們非常感謝。

+0

爲您的視圖提供清晰的背景色。 – rmaddy

+0

我該怎麼辦? 'NSColor.clearColor().set()'沒有幫助... –

+0

對不起,我在考慮iOS中'UIView'的'backgroundColor'屬性。我不確定是否有類似的'NSView'。 – rmaddy

回答

2

你應該做你的NSWindow實例如下:

[window setOpaque:NO]; 
[window setBackgroundColor:[NSColor clearColor]]; 

和借鑑需要的形狀。

並檢查this article

2

我找到了一個解決方案,通過使用一個子圖層。

class StyledButton: NSView { 
    let roundLayer: CALayer = CALayer() 

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

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setup() 
    } 

    func setup() { 
     self.wantsLayer = true 
     self.layer?.addSublayer(roundLayer) 
     roundLayer.frame = self.bounds 
     roundLayer.cornerRadius = 3 
     roundLayer.backgroundColor = NSColor.redColor().CGColor 
    } 
}