2016-11-16 74 views
2

我在屏幕的頂部有一個UIView,我想爲其添加一個像素的右邊框。如何在Swift 3的UIView上添加右邊框?

下面是我得到的時刻代碼:

var border = CALayer() 
border.backgroundColor = UIColor(red: 241, green: 10, blue: 9, alpha: 1).cgColor 
border.frame = CGRect(x: topView.frame.width + 1, y: 0, width: 1, height: topView.frame.height) 
topView.layer.addSublayer(border) 

,但我無法得到它的工作。

如何爲我的UIView添加正確的邊框?

在此先感謝!

回答

5

此代碼的工作對我來說,你是設置框的位置的視圖的邊界之外的內行,將其更改爲 - 1(而不是寬度+ 1),然後它應該按預期顯示。 (我改變了顏色,使其更容易在操場上看到的)

let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) 
view.backgroundColor = UIColor.white 

let borderLayer = CALayer() 
borderLayer.backgroundColor = UIColor.red.cgColor 
borderLayer.frame = CGRect(x: view.frame.width - 1, y: 0, width: 1, height: view.frame.height) 
view.layer.addSublayer(borderLayer) 
+0

好吧,在我的真實的例子,我用255,255,255作爲背景爲我的邊框(白色)。我現在感覺完全傻了。謝謝! –

0

Cliptobounds =虛假的俯視圖或保持架