Reputation: 664
I have to create a layout programmatically with several buttons, I create my buttons using the following code:
let myButton = UIButton(type: .Custom)
myButton.frame = CGRectMake(100,100,100,100)
myButton.setTitle("Normal", forState: .Normal)
myButton.setTitle("Selected", forState: [.Selected,.Highlighted])
myButton.setBackgroundImage(UIImage(named: "normal.png"), forState: .Normal)
myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Selected,.Highlighted])
self.view.addSubview(myButton)
The title and image are not changing once pressed.
The same code works if I refer to a UIButton created in the Storyboard.
Is there any other property that I need to set?
Thank you
Upvotes: 0
Views: 1870
Reputation: 12053
Try splitting the combined setBackgroundImage
call into two:
myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Highlighted])
myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Selected])
Upvotes: 6