Enhancing UIButton: Achieving a More Visible Press Effect

39 Views Asked by At

I've noticed that a default, standard, plain-style button doesn't provide a strong indication when it is pressed. For example, consider this plain-style button with its default settings. As you can see, it does not offer a clear visual indicator to show that it is currently being pressed.

enter image description here

enter image description here

When examining buttons provided by the OS, such as a photo picker, the visual effect when pressed is more obvious. Could you advise on how I can achieve a similar effect in my UIButton? Thank you.

enter image description here

1

There are 1 best solutions below

2
son On BEST ANSWER

You can simply config setTitleColor for each state with style == default. However, with Plain, these configurations do not work. So, you need to apply one of these approaches:

1. Change style to Default, then it will automatically work as expected

2. Using new button's UIConfiguration API from iOS 15

var config = button.configuration

config?.titleTextAttributesTransformer = config?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { [weak self] input in
    var output = input
    guard let button = self?.button else { return output }
    switch button.state {
    case .highlighted:
        output.foregroundColor = .systemBlue.withAlphaComponent(0.2)
    default:
        output.foregroundColor = .systemBlue
    }
    return output
}

button.configuration = config