UIButton 标签未显示

And*_* M. 3 uibutton uilabel ios swift

我有以下代码

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)
Run Code Online (Sandbox Code Playgroud)

当视图加载时,我看到以下图片(注意按钮是黑色矩形): 在此处输入图片说明

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

func linkButtonPressed(sender: UIButton) {
    print("button title = \(sender.titleLabel?.text)")
}
Run Code Online (Sandbox Code Playgroud)

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

Bhu*_*ika 5

您的按钮标题显示但为白色。为按钮标题提供任何颜色。您的代码没有问题。

linkButton.setTitle("http://example.com", forState: .Normal)
linkButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)