swiftuibutton

Swift UIButton not appearing on screen


I have a view in my tabbar controller where I would like to show a button. I create this button programmatically based of a condition, therefore I use the following code but nothing is appearing:

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!) {

}

I am clueless on what I am doing wrong. Anyone got suggestions? Cause it does print out "Button created" so it definitely runs the code inside the noConnected() method.


Solution

  • Add a background color to your UIButton and add a tint color to the title. This will resolve the problem