2016-01-10 62 views
1

我想插入我的標籤之一下方的彩色線:如何在swift中將底部邊框添加到標籤作爲子圖層?

let label = UILabel(frame: CGRectMake(0, 0, 70, 40)) 
label.text = items[index - 1] 
label.backgroundColor = UIColor.clearColor() 
label.textAlignment = .Center 
label.font = UIFont(name: "Helvetica", size: 15) 
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor 
label.translatesAutoresizingMaskIntoConstraints = false 

var sublayer = label.layer; 
sublayer.backgroundColor = UIColor.blueColor().CGColor 
sublayer.frame = CGRectMake(0, 0, label.frame.width, 1); 
sublayer.borderColor = UIColor.blackColor().CGColor; 
sublayer.borderWidth = 1; 
self.layer.insertSublayer(sublayer, atIndex: 0) 

self.addSubview(label) 

如何設置框正確,以便有我的標籤下方的彩色線?

回答

2

最終使用一個UIView:

let lineView = UIView(frame: CGRectMake(0, 
         self.frame.height - 3, 
         label.frame.width, 
         3.0)) 

lineView.backgroundColor = UIColor.blackColor() 
self.addSubview(lineView) 

的作品真的很好。

0

夫特3:

let lineView = UIView(frame: CGRect(x: 0, y: self.frame.height - 3, width: frame.width, height: 3.0)) 
lineView.backgroundColor = UIColor.black 
self.addSubview(lineView) 
相關問題