2016-07-07 52 views
1

我使用的是swift 2.2。我有一個視圖,我想在視圖的內邊框上使用漸變顏色。請幫助我。我使用以下代碼。在此處添加截圖我需要查看。在視圖的邊框上添加漸變顏色

self.layer.borderWidth = 1 

self.layer.borderColor = UIColor(white:0.1, alpha: 0.5).CGColor 

enter image description here

回答

0

試試這個代碼

@IBOutlet weak var gradientView: UIView! 

     gradientView.backgroundColor = UIColor.clearColor() 
     let gradient:CAGradientLayer = CAGradientLayer() 

     gradient.startPoint = CGPoint(x: 0.0, y: 1.0) 
     gradient.endPoint = CGPoint(x: 0.0, y: 0.0) 
     gradient.frame.size = self.gradientView.frame.size 
     gradient.colors = [UIColor.darkGrayColor().CGColor,UIColor.darkGrayColor().colorWithAlphaComponent(0).CGColor] //Or any colors 
     gradient.locations = [ 0.0, 1.0] 

     self.gradientView.layer.insertSublayer(gradient, atIndex: 0) 
0

iPatel的回答會的工作,但如果你想使用漸變功能,而不是一個圖像,你可以簡單地把一個視圖漸變填充隱藏在您的內容中的稍小視圖。只要較小的前視圖從梯度視圖中以相同的距離在所有側面插入,那麼這會給出您正在尋找的效果。

+0

您能否給我一些代碼行 – IKKA

+0

個人而言,我不會爲此編寫代碼。我會使用界面生成器並將視圖中的前視圖固定在具有漸變的視圖中。如果你想以編程的方式來做,那麼代碼將取決於你的確切用例。有很多在線教程可以幫助您快速定義漸變。 – Westside