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
In case you subclass UIButton and override its layoutSubviews()
, make sure you call super.layoutSubviews()
inside
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With