Srinidhi Srinidhi - 1 year ago 125
Objective-C Question

Automatically disable UIButton with type custom when a UIAlertController covers the screen

I have a

containing multiple
's of type
. In cases where my view looses focus like a new view controller gets presented or some other view is added on top of it, how do I gray out(disable) my custom buttons automatically?

Basically I want to gray out the buttons which cannot receive touch and so the user focus is only on the interactive elements on the screen.

I am not using a button type with system because I want to set non-monochrome images for button image.


Answer Source

When UIAlertController shows up this changes tint color of the window. That is why non custom buttons are dimmed. These normal and dimmed colors are taken from tintColor property. In case of a custom button you can override tintColorDidChange method:

class CustomButton: UIButton {

    override func tintColorDidChange() {
        if tintAdjustmentMode == .dimmed {
            // modify subviews to look disabled
        } else {
            // modify subviews to look enabled


But in your case it looks like your image has just one color. And that is why you can make it simpler. Just load button's image with rendering mode UIImageRenderingMode.alwaysTemplate and change button's tintColor be equal to the image's color. Then everything will happen automatically.

Making image be rendered in template mode is pssible with a help of Xcode enter image description here

or get it programmatically originalImage.withRenderingMode(.alwaysTemplate) from original image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download