我刚刚开始在Swift开发.
我动态创建了一个UIButton并将其添加到View中.我的问题是视图没有显示按钮.
我的代码:
var button:UIButton = UIButton(); button.frame = CGRect(x: 100, y: 250, width: 100, height: 50); button.setTitle("Midhun", forState: UIControlState.Normal); self.view.addSubview(button);
然后我将图像添加到按钮,然后显示:
button.setImage(UIImage(named: "step_first.jpg"), forState: UIControlState.Normal);
我无法弄清楚发生了什么.请帮忙
我实际上发现了这个问题.
在Swift中,标题UIButton
是白色的.
我更改了父View的背景颜色,现在显示了.
self.view.backgroundColor = UIColor.black
如果您继承UIButton并覆盖它layoutSubviews()
,请确保super.layoutSubviews()
在内部调用
对我来说问题是我没有使用setter方法.
我正在做notificationsButton.titleLabel?.text = "x"
而不是notificationsButton.setTitle("x", forState: UIControlState.Normal)