文本没有出现在swift中的UIButton上

时间:2016-04-03 13:34:58

标签: ios swift uibutton

猛烈抨击我。应该这么简单。

MyClass:

内的游乐场中跑步
func configureButton(){
    let btn: UIButton = UIButton(frame: CGRectMake(5, 75, 90, 20))
    btn.backgroundColor = UIColor.greenColor()
    btn.addTarget(self,
                  action: #selector(MyClass.buttonTapped),
                  forControlEvents: UIControlEvents.TouchUpInside)
    addSubview(btn)
    btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
    btn.titleLabel?.font = UIFont(name: "Helvetica", size: 40)
    btn.titleLabel?.text = "tap me"
  }

按钮显示绿色,按下时有效,但不显示文字。

1 个答案:

答案 0 :(得分:5)

这一行是错的:

btn.titleLabel?.text = "tap me"

请勿尝试直接操作按钮的标题标签文字。始终通过按钮的官方标题设置器setTitle:forState:

相关问题