我正在尝试在我的一个标签下插入彩色线条:
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)
如何正确设置框架,以便在我的标签下面有一条彩色线?
答案 0 :(得分:2)
使用uiview结束:
let lineView = UIView(frame: CGRectMake(0,
self.frame.height - 3,
label.frame.width,
3.0))
lineView.backgroundColor = UIColor.blackColor()
self.addSubview(lineView)
工作得很好。
答案 1 :(得分: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)