Swift UIButton没有出现在屏幕上

Res*_*had 3 uibutton swift

我的标签栏控制器中有一个视图,我想在其中显示一个按钮。我根据条件以编程方式创建此按钮,因此我使用以下代码,但未显示任何内容:

override func viewDidLoad() {
    super.viewDidLoad()
    if !Settings.getIsConnected() {
        notConnected()
    }
}

func notConnected() {
    let connectBtn = UIButton(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: 200, height: 45))
    connectBtn.setTitle("Connect", forState: .Normal)
    connectBtn.addTarget(self, action:#selector(self.pressedConnect(_:)), forControlEvents: .TouchUpInside)
    self.view.addSubview(connectBtn)

    print("Button created")
}

func pressedConnect(sender: UIButton!) {

}
Run Code Online (Sandbox Code Playgroud)

我对做错事一无所知。有人有建议吗?因为它确实打印出“创建的按钮”,所以它肯定在noConnected()方法内运行代码。

Ant*_*nyR 5

为您的商品添加背景色UIButton,为标题添加色彩。这样可以解决问题