Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton title not displaying


I just started developing in Swift.

I created an UIButton dynamically and added it to the View. My issue is the view is not displaying the button.

My Code:

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);

Then I added image to the button and then it is showing:

button.setImage(UIImage(named: "step_first.jpg"), forState: UIControlState.Normal);

I couldn't figure out what is happening. Please help

like image 278
Midhun MP Avatar asked Jun 03 '14 15:06

Midhun MP


3 Answers

In case you subclass UIButton and override its layoutSubviews(), make sure you call super.layoutSubviews() inside

like image 134
Nir Soudry Avatar answered Oct 11 '22 07:10

Nir Soudry


For me the issue was that I wasn't using the setter methods.

I was doing notificationsButton.titleLabel?.text = "x" instead of notificationsButton.setTitle("x", forState: UIControlState.Normal)

like image 26
cph2117 Avatar answered Oct 11 '22 09:10

cph2117


I actually figured out the issue.

In Swift the title of UIButton is white.

I changed the background color of parent View, now it is showing.

self.view.backgroundColor = UIColor.black
like image 43
Midhun MP Avatar answered Oct 11 '22 08:10

Midhun MP