0

I'm trying to make a button appear disabled without actually disabling it.

I want the greyed out color.

The button is Type Custom in interface builder. It has an image for the background.

I don't want to mess with image states, I simply want to change it to grey whenever I want in the same way that setting myButton.enabled = NO sets it to grey.

So I tried setting someButton.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed in my viewDidLoad, and even further into the app, and it's still appearing the typical bright blue color.

Is there some other property on the button I need to set/change in order to be able to control the tintAdjustmentMode myself?

I even tried my own button, using the following code, and that didn't work either.

@implementation TintableUIButton

    -(id)init
        {
            self = [super init];
            if (self) {
                self.tintColor = [UIColor grayColor];
                self.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
            }
            return self;
        }

@end
Logicsaurus Rex
  • 3,172
  • 2
  • 18
  • 27
  • Why don't you just set the UIButton color properties to the color of gray you are looking for. .tintColor / .titleColor etc. – Daniel Legler Mar 09 '17 at 19:24
  • @Daniel ~ That doesn't work either. `myButton.tintColor = [UIColor grayColor]` does nothing. Tint color is powerfully enforced throughout the hierarchy, and I'm trying to obtain an ounce of my own control over it. – Logicsaurus Rex Mar 09 '17 at 19:50
  • Custom button does not obey tint. You will have to recolor the image yourself. Here's an example where I do this for the text of the button title. https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch12p612tintColor/bk2ch12p612tintColor/MySpecialButton.swift – matt Mar 09 '17 at 22:19
  • @matt. Yes, it's looking like my only option is `myButton setImage:forState:` and just swap the precolored images. Oh well. – Logicsaurus Rex Mar 09 '17 at 23:35
  • Exactly the sort of thing I'm thinking of. – matt Mar 09 '17 at 23:35

2 Answers2

0

Try this,

You can create a custom class and add a method like:

class customBtnClass: UIButton {
     func fakeIsEnable(enable: Bool){
        if (enable){
            self.tintColor = .blue // your default tint
        }else{
            self.tintColor = .gray
        }
    }
}

Drag a simple button and set the custom class

Create an IBOutlet and call the method

@IBOutlet weak var btnTest: customBtnClass!
override func viewDidLoad() {
    super.viewDidLoad()
    btnTest.fakeIsEnable(enable: false)
}

Objective c:

-(void) fakeIsEnable:(BOOL) enable{
    if (enable){
        self.tintColor = [UIColor blueColor]; // your default tint
    }else{
        self.tintColor = [UIColor grayColor];
    }
}

In your ViewController (don't forget #import "CustomBtnClass.h" )

@interface ViewController ()
@property (weak, nonatomic) IBOutlet CustomBtnClass *yourButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.yourButton fakeIsEnable:NO];
}

Regards

agscastaneda
  • 174
  • 6
  • Updated my answer above, this didn't work. :( I'm just going to swap the image out for a recolored one as needed. I was hoping to use the tinting already built in when using `.enabled = NO` but it appears that functionality isn't available in the ways I've tried. – Logicsaurus Rex Mar 09 '17 at 23:36
-1

You can add a line of code on click event initially keep the button switchBool as false in viewdidload()

-(IBAction) btn_Action_Inside:(id)sender
{

    if (!switchBool) {

        [switchMap setBackgroundImage:[UIImage imageNamed:@"NormalImage"] forState:UIControlStateNormal];
        switchBool = true;
    }
    else if(switchBool)
    {

        [switchMap setBackgroundImage:[UIImage imageNamed:@"GrayedcolorImage"] forState:UIControlStateNormal];
        switchBool = false;
    }

}

Or you can opt for

-(IBAction)btn_Action_Inside:(id)sender
    {

        if (!switchBool) {

                self.yourButton.alpha = 1.0f;
                switchBool = true;
        }
        else if(switchBool)
        {

                self.yourButton.alpha = 0.5f;
                switchBool = false;
        }

    }

or you can opt this

-(IBAction)btn_Action_Inside:(id)sender
    {

        if (!switchBool) {


                [self.yourButton setTintColor:[UIColor redColor]];
                switchBool = true;
        }
        else if(switchBool)
        {

                [self.yourButton setTintColor:[UIColor grayColor]];
                switchBool = false;
        }

    }
  • Yes, I'm doing `setImage:forState:` per the comment in my original post. `setTintColor:` does not work as I and matt pointed out. – Logicsaurus Rex Mar 11 '17 at 04:01