Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIButton not appearing on screen

Tags:

swift

uibutton

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.

like image 899
Reshad Avatar asked Sep 14 '25 14:09

Reshad


1 Answers

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

like image 135
AnthonyR Avatar answered Sep 17 '25 06:09

AnthonyR