Andrey M. Andrey M. - 8 months ago 56
iOS Question

UIButton label not showing

I have the following code

let width = UIScreen.mainScreen().bounds.width
let linkLabel = UILabel(frame: CGRect(x: 8, y: contentHeight, width: 100, height: labelHeight))
let linkButton = UIButton(frame: CGRect(x: 108, y: contentHeight, width: width - 108, height: labelHeight))
linkLabel.text = "Link:"
linkButton.addTarget(self, action: #selector(linkButtonPressed), forControlEvents: .TouchUpInside)
linkButton.setTitle("", forState: .Normal)
linkButton.layer.borderWidth = 1.0

mainScrollView.contentSize = CGSize(width: view.frame.width, height: contentHeight)

And when view loads i see the following picture (Note that button is black rectangle):
enter image description here

So title is not visible but when i click on the button i can fetch its title property

func linkButtonPressed(sender: UIButton) {
print("button title = \(sender.titleLabel?.text)")

And i get the
button title = Optional("")
but anyway button title is not visible. Any suggestions?


You skipped one step. You should add title color :-

linkButton.setTitleColor(UIColor.blackColor(), forState: .Normal)