Midhun MP
Midhun MP

Reputation: 107231

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

Upvotes: 9

Views: 11528

Answers (3)

Midhun MP
Midhun MP

Reputation: 107231

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

Upvotes: 13

Nir Soudry
Nir Soudry

Reputation: 449

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

Upvotes: 18

cph2117
cph2117

Reputation: 2681

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)

Upvotes: 12

Related Questions