UIButton 标签不显示

UIButton label not showing

我有以下代码

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))
    mainScrollView.addSubview(linkLabel)
    mainScrollView.addSubview(linkButton)
    linkLabel.text = "Link:"
    linkButton.addTarget(self, action: #selector(linkButtonPressed), forControlEvents: .TouchUpInside)
    linkButton.setTitle("http://example.com", forState: .Normal)
    linkButton.layer.borderWidth = 1.0

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

当视图加载时,我看到了下图(请注意按钮是黑色矩形):

所以标题不可见但是当我点击按钮时我可以获取它的标题属性

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

我得到了 button title = Optional("http://example.com") 但无论如何按钮标题不可见。有什么建议吗?

您的按钮标题显示为白色 color.Give 按钮标题的任何颜色 there.There 在您的代码中都没有问题。

linkButton.setTitle("http://example.com", forState: .Normal)
linkButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

您跳过了一步。您应该添加标题颜色:-

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