When I create a UIButton
in Swift, e.g. by
let aButton = UIButton()
aButton.setTitle("Title", forState: UIControlState.Normal)
it seems like I'm not getting the same button style as I would get if I added a button in Interface Builder. Specifically, the title color is white instead of the default "tint color", leaving the button invisible. Now I want this button to look like any other button, so I don't want to specify my own colors.
Setting the normal (not pressed) state can be done with:
aButton.setTitleColor(self.tintColor, forState: UIControlState.Normal)
So far, so good. But the text color does not change when the button is pressed. I guess, for that I need
aButton.setTitleColor(/* somehting here */, self.tintColor, forState: UIControlState.Highlighted)
Ideally without hardcoding the color, what do I need to substitute above to get the default pressed button color? Or is there even a simpler way to just create a UIButton
with default style?
In order to declare a button of the same (default) style as in the storyboard/interface builder, use the following code to instantiate it:
let aButton = UIButton(type: .system)
Otherwise the button is of type custom
.
You can read about the various button types here.
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