UIButton标签没有显示

时间:2016-05-30 08:40:53

标签: ios swift uibutton uilabel

我有以下代码

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)

当视图加载时,我会看到下面的图片(注意按钮是黑色矩形): enter image description here

因此标题不可见但是当我点击按钮时我可以获取其标题属性

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

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

2 个答案:

答案 0 :(得分:3)

你的按钮标题是显示的,但是是白色。给那里的按钮标题添加任何颜色。你的代码没有问题。

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

答案 1 :(得分:3)

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

linkButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
相关问题