Reputation: 3548
I am trying to insert a colored line under one of my labels :
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)
How can I set the frame properly so that there is a colored line under my label ?
Upvotes: 2
Views: 4539
Reputation: 169
Swift 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)
Upvotes: 2
Reputation: 3548
Ended up using a uiview :
let lineView = UIView(frame: CGRectMake(0,
self.frame.height - 3,
label.frame.width,
3.0))
lineView.backgroundColor = UIColor.blackColor()
self.addSubview(lineView)
Works really well.
Upvotes: 2